Search in sources :

Example 1 with FileAttachment

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;
}
Also used : ZonedDateTime(java.time.ZonedDateTime) FileAttachment(io.lumeer.api.model.FileAttachment) Document(org.bson.Document) Date(java.util.Date)

Example 2 with 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);
}
Also used : Role(io.lumeer.api.model.Role) Group(io.lumeer.api.model.Group) Project(io.lumeer.api.model.Project) User(io.lumeer.api.model.User) AuthenticatedUser(io.lumeer.core.auth.AuthenticatedUser) Organization(io.lumeer.api.model.Organization) FileAttachment(io.lumeer.api.model.FileAttachment) Permissions(io.lumeer.api.model.Permissions) Collection(io.lumeer.api.model.Collection) Before(org.junit.Before)

Example 3 with FileAttachment

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);
}
Also used : Entity(javax.ws.rs.client.Entity) FileAttachment(io.lumeer.api.model.FileAttachment) Test(org.junit.Test)

Example 4 with FileAttachment

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);
}
Also used : FileAttachment(io.lumeer.api.model.FileAttachment)

Example 5 with 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());
}
Also used : ZonedDateTime(java.time.ZonedDateTime) LinkTypeDao(io.lumeer.storage.api.dao.LinkTypeDao) LinkInstanceUtils(io.lumeer.core.util.LinkInstanceUtils) HashSet(java.util.HashSet) Inject(javax.inject.Inject) LumeerS3Client(io.lumeer.core.util.LumeerS3Client) CollectionDao(io.lumeer.storage.api.dao.CollectionDao) DataDao(io.lumeer.storage.api.dao.DataDao) Map(java.util.Map) URI(java.net.URI) InvalidValueException(io.lumeer.engine.api.exception.InvalidValueException) DocumentDao(io.lumeer.storage.api.dao.DocumentDao) Document(io.lumeer.api.model.Document) Set(java.util.Set) Collectors(java.util.stream.Collectors) LinkInstance(io.lumeer.api.model.LinkInstance) FileAttachmentAdapter(io.lumeer.core.adapter.FileAttachmentAdapter) LinkType(io.lumeer.api.model.LinkType) DocumentUtils(io.lumeer.core.util.DocumentUtils) FileAttachment(io.lumeer.api.model.FileAttachment) LinkInstanceDao(io.lumeer.storage.api.dao.LinkInstanceDao) DefaultConfigurationProducer(io.lumeer.core.facade.configuration.DefaultConfigurationProducer) StorageException(io.lumeer.storage.api.exception.StorageException) List(java.util.List) FileAttachmentDao(io.lumeer.storage.api.dao.FileAttachmentDao) RequestScoped(javax.enterprise.context.RequestScoped) PostConstruct(javax.annotation.PostConstruct) LinkDataDao(io.lumeer.storage.api.dao.LinkDataDao) Collections(java.util.Collections) Collection(io.lumeer.api.model.Collection) FileAttachment(io.lumeer.api.model.FileAttachment)

Aggregations

FileAttachment (io.lumeer.api.model.FileAttachment)10 Collection (io.lumeer.api.model.Collection)4 FileAttachmentAdapter (io.lumeer.core.adapter.FileAttachmentAdapter)4 LumeerS3Client (io.lumeer.core.util.LumeerS3Client)4 ZonedDateTime (java.time.ZonedDateTime)4 Document (io.lumeer.api.model.Document)3 LinkInstance (io.lumeer.api.model.LinkInstance)3 LinkType (io.lumeer.api.model.LinkType)3 DefaultConfigurationProducer (io.lumeer.core.facade.configuration.DefaultConfigurationProducer)3 DocumentUtils (io.lumeer.core.util.DocumentUtils)3 LinkInstanceUtils (io.lumeer.core.util.LinkInstanceUtils)3 InvalidValueException (io.lumeer.engine.api.exception.InvalidValueException)3 CollectionDao (io.lumeer.storage.api.dao.CollectionDao)3 DataDao (io.lumeer.storage.api.dao.DataDao)3 DocumentDao (io.lumeer.storage.api.dao.DocumentDao)3 FileAttachmentDao (io.lumeer.storage.api.dao.FileAttachmentDao)3 LinkDataDao (io.lumeer.storage.api.dao.LinkDataDao)3 LinkInstanceDao (io.lumeer.storage.api.dao.LinkInstanceDao)3 LinkTypeDao (io.lumeer.storage.api.dao.LinkTypeDao)3 StorageException (io.lumeer.storage.api.exception.StorageException)3