Skip to content
Snippets Groups Projects
Commit edff42ec authored by Nicolas.Rod's avatar Nicolas.Rod
Browse files

feat(DocumentFile): new endpoint open to ROOT to calculate a document file checksum

parent e31f7e30
No related branches found
No related tags found
No related merge requests found
......@@ -24,6 +24,7 @@
package ch.unige.aou.business;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
......@@ -465,6 +466,34 @@ public class DocumentFileService extends AouResourceService<DocumentFile> {
return ((DocumentFileRepository) this.itemRepository).getDocumentFilesWithEmbargoEndDateEnding(startDate, endDate);
}
public DocumentFile completeChecksum(String id) {
DocumentFile documentFile = this.findOne(id);
try {
if (documentFile.isStoredOnFileSystem()) {
return this.completeChecksumForLocalFile(documentFile);
} else if (documentFile.isPublicationImportedFromFedora()) {
return this.completeChecksumByDownloadingFile(documentFile);
} else {
throw new SolidifyRuntimeException(
"Completing checksum for document file not supported (finalData: '" + documentFile.getFinalData() + "') (file: "
+ documentFile.getResId() + ")");
}
} catch (NoSuchAlgorithmException | IOException e) {
throw new SolidifyRuntimeException(
"Unable to complete checksum for document file (finalData: '" + documentFile.getFinalData() + "') (file: "
+ documentFile.getResId() + ")", e);
}
}
public DocumentFile completeChecksumForLocalFile(DocumentFile documentFile) throws IOException, NoSuchAlgorithmException {
String checksumDf = ChecksumTool.computeChecksum(new BufferedInputStream(new FileInputStream(new File(documentFile.getFinalData())),
SolidifyConstants.BUFFER_SIZE), ChecksumAlgorithm.SHA256);
documentFile.setChecksum(checksumDf);
DocumentFile savedDocumentFile = this.save(documentFile);
log.info("checksum calculated for document file (finalData: '{}') (file: {})", documentFile.getFinalData(), documentFile.getResId());
return savedDocumentFile;
}
public DocumentFile completeChecksumByDownloadingFile(DocumentFile documentFile) throws IOException, NoSuchAlgorithmException {
Path tmpFilePath = this.downloadFileInTempFolder(documentFile);
String checksum = ChecksumTool.computeChecksum(
......@@ -472,7 +501,8 @@ public class DocumentFileService extends AouResourceService<DocumentFile> {
documentFile.setChecksum(checksum);
DocumentFile savedDocumentFile = this.save(documentFile);
FileTool.deleteFile(tmpFilePath);
log.info("checksum calculated by downloading file for document file {}", documentFile.getResId());
log.info("checksum calculated by downloading file for document file (finalData: '{}') (file: {})", documentFile.getFinalData(),
documentFile.getResId());
return savedDocumentFile;
}
......
......@@ -9,12 +9,12 @@
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
......@@ -24,10 +24,12 @@
package ch.unige.aou.controller.admin;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
......@@ -35,6 +37,7 @@ import org.springframework.web.bind.annotation.RestController;
import ch.unige.solidify.SolidifyConstants;
import ch.unige.solidify.controller.ResourceReadOnlyController;
import ch.unige.solidify.exception.SolidifyHttpErrorException;
import ch.unige.solidify.security.RootPermissions;
import ch.unige.solidify.security.TrustedUserPermissions;
import ch.unige.aou.business.DocumentFileService;
......@@ -68,4 +71,11 @@ public class DocumentFileController extends ResourceReadOnlyController<DocumentF
return super.get(id);
}
@RootPermissions
@PostMapping(SolidifyConstants.URL_ID_PLUS_SEP + AouActionName.CALCULATE_CHECKSUM)
public HttpEntity<String> calculateChecksum(@PathVariable String id) {
DocumentFile documentFile = ((DocumentFileService) this.itemService).completeChecksum(id);
return new ResponseEntity<>(documentFile.getChecksum(), HttpStatus.OK);
}
}
......@@ -169,6 +169,8 @@ public class AouActionName {
public static final String CHECK_DUPLICATES = "check-duplicates";
public static final String IMPORT = "import";
public static final String CALCULATE_CHECKSUM = "calculate-checksum";
public static final String CLEAN_CACHES = "clean-caches";
private AouActionName() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment