use of io.lumeer.api.model.FileAttachment in project engine by Lumeer.
the class FileAttachmentCodec method decode.
@Override
public FileAttachment decode(final BsonReader reader, final DecoderContext decoderContext) {
final Document bson = documentCodec.decode(reader, decoderContext);
final String id = bson.getObjectId(ID).toHexString();
final String organizationId = bson.getString(ORGANIZATION_ID);
final String projectId = bson.getString(PROJECT_ID);
final String collectionId = bson.getString(COLLECTION_ID);
final String documentId = bson.getString(DOCUMENT_ID);
final String attributeId = bson.getString(ATTRIBUTE_ID);
final String fileName = bson.getString(FILE_NAME);
final String uniqueNameString = bson.getString(UNIQUE_NAME);
final String uniqueName = uniqueNameString != null ? uniqueNameString : fileName;
final Integer typeInt = bson.getInteger(ATTACHMENT_TYPE);
final FileAttachment.AttachmentType type = FileAttachment.AttachmentType.values()[typeInt != null ? typeInt : 0];
Date creationDate = bson.getDate(CREATION_DATE);
ZonedDateTime creationZonedDate = creationDate != null ? ZonedDateTime.ofInstant(creationDate.toInstant(), ZoneOffset.UTC) : null;
String createdBy = bson.getString(CREATED_BY);
final FileAttachment fileAttachment = new FileAttachment(organizationId, projectId, collectionId, documentId, attributeId, fileName, uniqueName, type);
fileAttachment.setId(id);
fileAttachment.setCreatedBy(createdBy);
fileAttachment.setCreationDate(creationZonedDate);
return fileAttachment;
}
use of io.lumeer.api.model.FileAttachment in project engine by Lumeer.
the class FileAttachmentFacadeIT method configureProject.
@Before
public void configureProject() {
User user = new User(USER);
this.user = userDao.createUser(user);
Organization organization = new Organization();
organization.setCode(ORGANIZATION_CODE);
Permissions organizationPermissions = new Permissions();
userPermission = Permission.buildWithRoles(this.user.getId(), Organization.ROLES);
organizationPermissions.updateUserPermissions(userPermission);
organization.setPermissions(organizationPermissions);
this.organization = organizationDao.createOrganization(organization);
projectDao.setOrganization(this.organization);
groupDao.setOrganization(this.organization);
Group group = new Group(GROUP);
this.group = groupDao.createGroup(group);
userPermission = Permission.buildWithRoles(this.user.getId(), Collection.ROLES);
groupPermission = Permission.buildWithRoles(this.group.getId(), Collections.singleton(new Role(RoleType.Read)));
Project project = new Project();
project.setCode(PROJECT_CODE);
Permissions projectPermissions = new Permissions();
projectPermissions.updateUserPermissions(Permission.buildWithRoles(this.user.getId(), Project.ROLES));
project.setPermissions(projectPermissions);
this.project = projectDao.createProject(project);
workspaceKeeper.setWorkspaceIds(this.organization.getId(), this.project.getId());
collectionDao.setProject(project);
collection = new Collection("C1", "My collection", "fa-eye", "ffaabb", null);
collection.getPermissions().updateUserPermissions(userPermission);
collection.getPermissions().updateGroupPermissions(groupPermission);
collection = collectionDao.createCollection(collection);
fileAttachment1 = new FileAttachment(organization.getId(), project.getId(), collection.getId(), "5cf6f208857aba009210af9b", "a3", "můk super/$~@#ę€%=název.pdf", "", FileAttachment.AttachmentType.DOCUMENT);
fileAttachment2 = new FileAttachment(organization.getId(), "5cf6f208857aba009210af9c", collection.getId(), "5cf6f208857aba009210af9b", "a3", "normal file name .doc", "", FileAttachment.AttachmentType.DOCUMENT);
}
use of io.lumeer.api.model.FileAttachment in project engine by Lumeer.
the class FileAttachmentFacadeIT method testFileUpload.
@Test
public void testFileUpload() {
final FileAttachment createdFileAttachment = fileAttachmentFacade.createFileAttachment(fileAttachment1);
assertThat(createdFileAttachment.getPresignedUrl()).isNotEmpty();
final String content = "Hello world text file";
final Entity entity = Entity.text(content);
final String presigned = createdFileAttachment.getPresignedUrl();
var response = client.target(presigned).request(MediaType.APPLICATION_JSON).buildPut(entity).invoke();
var result = fileAttachmentFacade.getAllFileAttachments(createdFileAttachment.getCollectionId(), FileAttachment.AttachmentType.DOCUMENT);
assertThat(result).containsExactly(createdFileAttachment);
var listing = fileAttachmentFacade.listFileAttachments(createdFileAttachment.getCollectionId(), createdFileAttachment.getDocumentId(), createdFileAttachment.getAttributeId(), FileAttachment.AttachmentType.DOCUMENT);
var tempFileAttachment = new FileAttachment(createdFileAttachment.getOrganizationId(), createdFileAttachment.getProjectId(), createdFileAttachment.getCollectionId(), createdFileAttachment.getDocumentId(), createdFileAttachment.getAttributeId(), createdFileAttachment.getFileName(), createdFileAttachment.getUniqueName(), FileAttachment.AttachmentType.DOCUMENT);
tempFileAttachment.setSize(content.length());
assertThat(listing).containsExactly(tempFileAttachment);
assertThat(listing.get(0).getSize()).isGreaterThan(0L);
fileAttachmentFacade.removeFileAttachment(createdFileAttachment);
listing = fileAttachmentFacade.listFileAttachments(createdFileAttachment.getCollectionId(), createdFileAttachment.getDocumentId(), createdFileAttachment.getAttributeId(), FileAttachment.AttachmentType.DOCUMENT);
assertThat(listing).hasSize(0);
}
use of io.lumeer.api.model.FileAttachment in project engine by Lumeer.
the class FileAttachmentFacade method renameFileAttachment.
public FileAttachment renameFileAttachment(final FileAttachment fileAttachment) {
if (fileAttachment.getAttachmentType().equals(FileAttachment.AttachmentType.DOCUMENT)) {
checkCanEditDocument(fileAttachment.getCollectionId(), fileAttachment.getDocumentId());
} else {
checkCanEditLinkInstance(fileAttachment.getCollectionId(), fileAttachment.getDocumentId());
}
final FileAttachment storedFileAttachment = fileAttachmentDao.findFileAttachment(fileAttachment);
if (storedFileAttachment.getFileName().equals(fileAttachment.getFileName())) {
return fileAttachment;
}
checkFileAttachmentName(fileAttachment);
storedFileAttachment.setFileName(fileAttachment.getFileName());
return fileAttachmentDao.updateFileAttachment(fileAttachment);
}
use of io.lumeer.api.model.FileAttachment in project engine by Lumeer.
the class FileAttachmentFacade method listFileAttachments.
/**
* Lists the file attachments really present in the S3 bucket.
*
* @param collectionId ID of a collection.
* @param documentId ID of a document.
* @param attributeId ID of an attribute.
* @param type the type of file attachment.
* @return Files present in S3 bucket for the specified collection, document and its attribute.
*/
public List<FileAttachment> listFileAttachments(final String collectionId, final String documentId, final String attributeId, final FileAttachment.AttachmentType type) {
if (!lumeerS3Client.isInitialized()) {
return Collections.emptyList();
}
if (type.equals(FileAttachment.AttachmentType.DOCUMENT)) {
checkCanReadDocument(collectionId, documentId);
} else {
checkCanReadLinkInstance(collectionId, documentId);
}
final String organizationId = getOrganization().getId();
final String projectId = getProject().getId();
return lumeerS3Client.listObjects(adapter.getFileAttachmentLocation(organizationId, projectId, collectionId, documentId, attributeId, type)).stream().map(s3ObjectItem -> {
final FileAttachment fileAttachment = new FileAttachment(organizationId, projectId, collectionId, documentId, attributeId, s3ObjectItem.getKey(), s3ObjectItem.getKey(), type);
fileAttachment.setSize(s3ObjectItem.getSize());
return fileAttachment;
}).collect(Collectors.toList());
}
Aggregations