fix description storage file

This commit is contained in:
Efstratios Giannopoulos 2024-04-30 11:01:39 +03:00
parent 787aa31d53
commit 33196e28cd
3 changed files with 46 additions and 49 deletions

View File

@ -1,10 +1,10 @@
package org.opencdmp.data; package org.opencdmp.data;
import jakarta.persistence.*;
import org.opencdmp.commons.enums.StorageType; import org.opencdmp.commons.enums.StorageType;
import org.opencdmp.data.converters.enums.StorageTypeConverter; import org.opencdmp.data.converters.enums.StorageTypeConverter;
import org.opencdmp.data.tenant.TenantScopedBaseEntity; import org.opencdmp.data.tenant.TenantScopedBaseEntity;
import jakarta.persistence.*;
import java.time.Instant; import java.time.Instant;
import java.util.UUID; import java.util.UUID;
@ -60,7 +60,7 @@ public class StorageFileEntity extends TenantScopedBaseEntity {
public final static String _ownerId = "ownerId"; public final static String _ownerId = "ownerId";
public UUID getId() { public UUID getId() {
return id; return this.id;
} }
public void setId(UUID id) { public void setId(UUID id) {
@ -68,7 +68,7 @@ public class StorageFileEntity extends TenantScopedBaseEntity {
} }
public String getFileRef() { public String getFileRef() {
return fileRef; return this.fileRef;
} }
public void setFileRef(String fileRef) { public void setFileRef(String fileRef) {
@ -76,7 +76,7 @@ public class StorageFileEntity extends TenantScopedBaseEntity {
} }
public String getName() { public String getName() {
return name; return this.name;
} }
public void setName(String name) { public void setName(String name) {
@ -84,7 +84,7 @@ public class StorageFileEntity extends TenantScopedBaseEntity {
} }
public String getExtension() { public String getExtension() {
return extension; return this.extension;
} }
public void setExtension(String extension) { public void setExtension(String extension) {
@ -92,7 +92,7 @@ public class StorageFileEntity extends TenantScopedBaseEntity {
} }
public String getMimeType() { public String getMimeType() {
return mimeType; return this.mimeType;
} }
public void setMimeType(String mimeType) { public void setMimeType(String mimeType) {
@ -100,7 +100,7 @@ public class StorageFileEntity extends TenantScopedBaseEntity {
} }
public StorageType getStorageType() { public StorageType getStorageType() {
return storageType; return this.storageType;
} }
public void setStorageType(StorageType storageType) { public void setStorageType(StorageType storageType) {
@ -109,7 +109,7 @@ public class StorageFileEntity extends TenantScopedBaseEntity {
public Instant getCreatedAt() { public Instant getCreatedAt() {
return createdAt; return this.createdAt;
} }
public void setCreatedAt(Instant createdAt) { public void setCreatedAt(Instant createdAt) {
@ -117,7 +117,7 @@ public class StorageFileEntity extends TenantScopedBaseEntity {
} }
public Instant getPurgeAt() { public Instant getPurgeAt() {
return purgeAt; return this.purgeAt;
} }
public void setPurgeAt(Instant purgeAt) { public void setPurgeAt(Instant purgeAt) {
@ -125,7 +125,7 @@ public class StorageFileEntity extends TenantScopedBaseEntity {
} }
public Instant getPurgedAt() { public Instant getPurgedAt() {
return purgedAt; return this.purgedAt;
} }
public void setPurgedAt(Instant purgedAt) { public void setPurgedAt(Instant purgedAt) {
@ -133,7 +133,7 @@ public class StorageFileEntity extends TenantScopedBaseEntity {
} }
public UUID getOwnerId() { public UUID getOwnerId() {
return ownerId; return this.ownerId;
} }
public void setOwnerId(UUID ownerId) { public void setOwnerId(UUID ownerId) {

View File

@ -1,5 +1,19 @@
package org.opencdmp.service.description; package org.opencdmp.service.description;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.deleter.DeleterFactory;
import gr.cite.tools.data.query.Ordering;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.*;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import gr.cite.tools.validation.ValidatorFactory;
import jakarta.xml.bind.JAXBException;
import org.apache.commons.io.FilenameUtils;
import org.jetbrains.annotations.NotNull;
import org.opencdmp.authorization.AffiliatedResource; import org.opencdmp.authorization.AffiliatedResource;
import org.opencdmp.authorization.AuthorizationFlags; import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.authorization.Permission; import org.opencdmp.authorization.Permission;
@ -7,6 +21,7 @@ import org.opencdmp.authorization.authorizationcontentresolver.AuthorizationCont
import org.opencdmp.commons.JsonHandlingService; import org.opencdmp.commons.JsonHandlingService;
import org.opencdmp.commons.XmlHandlingService; import org.opencdmp.commons.XmlHandlingService;
import org.opencdmp.commons.enums.*; import org.opencdmp.commons.enums.*;
import org.opencdmp.commons.notification.NotificationProperties;
import org.opencdmp.commons.scope.tenant.TenantScope; import org.opencdmp.commons.scope.tenant.TenantScope;
import org.opencdmp.commons.scope.user.UserScope; import org.opencdmp.commons.scope.user.UserScope;
import org.opencdmp.commons.types.description.*; import org.opencdmp.commons.types.description.*;
@ -15,12 +30,8 @@ import org.opencdmp.commons.types.descriptionreference.DescriptionReferenceDataE
import org.opencdmp.commons.types.descriptiontemplate.FieldSetEntity; import org.opencdmp.commons.types.descriptiontemplate.FieldSetEntity;
import org.opencdmp.commons.types.descriptiontemplate.fielddata.ReferenceTypeDataEntity; import org.opencdmp.commons.types.descriptiontemplate.fielddata.ReferenceTypeDataEntity;
import org.opencdmp.commons.types.descriptiontemplate.fielddata.UploadDataEntity; import org.opencdmp.commons.types.descriptiontemplate.fielddata.UploadDataEntity;
import org.opencdmp.commons.types.dmp.DmpBlueprintValueEntity;
import org.opencdmp.commons.types.dmp.DmpContactEntity;
import org.opencdmp.commons.types.dmp.importexport.*;
import org.opencdmp.commons.types.notification.*; import org.opencdmp.commons.types.notification.*;
import org.opencdmp.commons.types.reference.DefinitionEntity; import org.opencdmp.commons.types.reference.DefinitionEntity;
import org.opencdmp.commons.notification.NotificationProperties;
import org.opencdmp.convention.ConventionService; import org.opencdmp.convention.ConventionService;
import org.opencdmp.data.*; import org.opencdmp.data.*;
import org.opencdmp.errorcode.ErrorThesaurusProperties; import org.opencdmp.errorcode.ErrorThesaurusProperties;
@ -43,24 +54,10 @@ import org.opencdmp.model.persist.referencedefinition.DefinitionPersist;
import org.opencdmp.query.*; import org.opencdmp.query.*;
import org.opencdmp.service.descriptiontemplate.DescriptionTemplateService; import org.opencdmp.service.descriptiontemplate.DescriptionTemplateService;
import org.opencdmp.service.elastic.ElasticService; import org.opencdmp.service.elastic.ElasticService;
import org.opencdmp.service.filetransformer.FileTransformerService;
import org.opencdmp.service.responseutils.ResponseUtilsService; import org.opencdmp.service.responseutils.ResponseUtilsService;
import org.opencdmp.service.storage.StorageFileProperties; import org.opencdmp.service.storage.StorageFileProperties;
import org.opencdmp.service.storage.StorageFileService; import org.opencdmp.service.storage.StorageFileService;
import org.opencdmp.service.filetransformer.FileTransformerService;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.deleter.DeleterFactory;
import gr.cite.tools.data.query.Ordering;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.*;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import gr.cite.tools.validation.ValidatorFactory;
import jakarta.xml.bind.JAXBException;
import org.apache.commons.io.FilenameUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
@ -510,13 +507,13 @@ public class DescriptionServiceImpl implements DescriptionService {
if (FieldType.UPLOAD.equals(fieldType)){ if (FieldType.UPLOAD.equals(fieldType)){
UUID newFileId = this.conventionService.isValidUUID(persist.getTextValue()) ? UUID.fromString(persist.getTextValue()) : null; UUID newFileId = this.conventionService.isValidUUID(persist.getTextValue()) ? UUID.fromString(persist.getTextValue()) : null;
if (newFileId != null){ if (newFileId != null){
StorageFileEntity existingFile = this.queryFactory.query(StorageFileQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(newFileId).firstAs(new BaseFieldSet().ensure(StorageFile._id)); StorageFileEntity storageFileEntity = this.queryFactory.query(StorageFileQuery.class).ids(newFileId).firstAs(new BaseFieldSet().ensure(StorageFile._id).ensure(StorageFile._storageType));
if (existingFile == null){ if (storageFileEntity == null || storageFileEntity.getStorageType().equals(StorageType.Temp)){
StorageFile storageFile = this.storageFileService.copyToStorage(newFileId, StorageType.Main, true, new BaseFieldSet().ensure(StorageFile._id)); StorageFile storageFile = this.storageFileService.copyToStorage(newFileId, StorageType.Main, true, new BaseFieldSet().ensure(StorageFile._id));
this.storageFileService.updatePurgeAt(storageFile.getId(), null); this.storageFileService.updatePurgeAt(storageFile.getId(), null);
data.setTextValue(storageFile.getId().toString()); data.setTextValue(storageFile.getId().toString());
} else { } else {
if (existingFile.getId() != null){ if (storageFileEntity.getId() != null){
//DO NOT Remove we can not be sure uf the description is copied //DO NOT Remove we can not be sure uf the description is copied
//this.storageFileService.updatePurgeAt(existingFileId, Instant.now().minusSeconds(60)); //this.storageFileService.updatePurgeAt(existingFileId, Instant.now().minusSeconds(60));
} }

View File

@ -1,18 +1,5 @@
package org.opencdmp.controllers; package org.opencdmp.controllers;
import org.opencdmp.audit.AuditableAction;
import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.authorization.Permission;
import org.opencdmp.commons.enums.StorageType;
import org.opencdmp.commons.scope.user.UserScope;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.data.StorageFileEntity;
import org.opencdmp.model.StorageFile;
import org.opencdmp.model.builder.StorageFileBuilder;
import org.opencdmp.model.persist.StorageFilePersist;
import org.opencdmp.query.StorageFileQuery;
import org.opencdmp.service.storage.StorageFileProperties;
import org.opencdmp.service.storage.StorageFileService;
import gr.cite.commons.web.authz.service.AuthorizationService; import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.auditing.AuditService; import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.data.builder.BuilderFactory; import gr.cite.tools.data.builder.BuilderFactory;
@ -26,6 +13,19 @@ import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry; import gr.cite.tools.logging.MapLogEntry;
import gr.cite.tools.validation.ValidatorFactory; import gr.cite.tools.validation.ValidatorFactory;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.opencdmp.audit.AuditableAction;
import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.authorization.Permission;
import org.opencdmp.commons.enums.StorageType;
import org.opencdmp.commons.scope.user.UserScope;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.data.StorageFileEntity;
import org.opencdmp.model.StorageFile;
import org.opencdmp.model.builder.StorageFileBuilder;
import org.opencdmp.model.persist.StorageFilePersist;
import org.opencdmp.query.StorageFileQuery;
import org.opencdmp.service.storage.StorageFileProperties;
import org.opencdmp.service.storage.StorageFileService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
@ -86,7 +86,7 @@ public class StorageFileController {
StorageFileQuery query = this.queryFactory.query(StorageFileQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id); StorageFileQuery query = this.queryFactory.query(StorageFileQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id);
StorageFile model = this.builderFactory.builder(StorageFileBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet)); StorageFile model = this.builderFactory.builder(StorageFileBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet));
if (model == null) if (model == null)
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, StorageFile.class.getSimpleName()}, LocaleContextHolder.getLocale())); throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, StorageFile.class.getSimpleName()}, LocaleContextHolder.getLocale()));
this.auditService.track(AuditableAction.StorageFile_Query, Map.ofEntries( this.auditService.track(AuditableAction.StorageFile_Query, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id), new AbstractMap.SimpleEntry<String, Object>("id", id),
@ -130,10 +130,10 @@ public class StorageFileController {
this.authorizationService.authorizeForce(Permission.BrowseStorageFile, Permission.DeferredAffiliation); this.authorizationService.authorizeForce(Permission.BrowseStorageFile, Permission.DeferredAffiliation);
StorageFileEntity storageFile = this.queryFactory.query(StorageFileQuery.class).ids(id).firstAs(new BaseFieldSet().ensure(StorageFile._createdAt, StorageFile._fullName, StorageFile._mimeType, StorageFile._extension)); StorageFileEntity storageFile = this.queryFactory.query(StorageFileQuery.class).ids(id).firstAs(new BaseFieldSet().ensure(StorageFile._createdAt, StorageFile._fullName, StorageFile._mimeType, StorageFile._extension));
if (storageFile == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, StorageFile.class.getSimpleName()}, LocaleContextHolder.getLocale())); if (storageFile == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, StorageFile.class.getSimpleName()}, LocaleContextHolder.getLocale()));
byte[] file = this.storageFileService.readAsBytesSafe(id); byte[] file = this.storageFileService.readAsBytesSafe(id);
if (file == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, StorageFile.class.getSimpleName()}, LocaleContextHolder.getLocale())); if (file == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, StorageFile.class.getSimpleName()}, LocaleContextHolder.getLocale()));
this.auditService.track(AuditableAction.StorageFile_Download, Map.ofEntries( this.auditService.track(AuditableAction.StorageFile_Download, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id) new AbstractMap.SimpleEntry<String, Object>("id", id)