Search in sources :

Example 1 with FileInfo

use of org.craftercms.commons.mongo.FileInfo in project profile by craftercms.

the class ProfileServiceImpl method getProfileAttachments.

@Override
public List<ProfileAttachment> getProfileAttachments(final String profileId) throws ProfileException {
    List<FileInfo> files = profileRepository.listFilesByName(profileId);
    List<ProfileAttachment> attachments = new ArrayList<>();
    for (FileInfo file : files) {
        attachments.add(fileInfoToProfileAttachment(file));
    }
    return attachments;
}
Also used : ProfileAttachment(org.craftercms.profile.api.services.ProfileAttachment) FileInfo(org.craftercms.commons.mongo.FileInfo) ArrayList(java.util.ArrayList)

Example 2 with FileInfo

use of org.craftercms.commons.mongo.FileInfo in project profile by craftercms.

the class ProfileServiceImpl method addProfileAttachment.

@Override
public ProfileAttachment addProfileAttachment(final String profileId, final String attachmentName, final InputStream file) throws ProfileException {
    String storeName = "/" + profileId + "/" + FilenameUtils.removeExtension(attachmentName);
    try {
        ObjectId currentId = checkIfAttachmentExist(storeName);
        String mimeType = getAttachmentContentType(attachmentName);
        FileInfo fileInfo;
        if (currentId != null) {
            // Update !!!
            fileInfo = profileRepository.updateFile(currentId, file, storeName, mimeType, true);
        } else {
            fileInfo = profileRepository.saveFile(file, storeName, mimeType);
        }
        return fileInfoToProfileAttachment(fileInfo);
    } catch (MongoDataException | FileExistsException | FileNotFoundException e) {
        throw new ProfileException("Unable to attach file to profile '" + profileId + "'", e);
    }
}
Also used : FileInfo(org.craftercms.commons.mongo.FileInfo) ObjectId(org.bson.types.ObjectId) FileNotFoundException(java.io.FileNotFoundException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) NoSuchProfileException(org.craftercms.profile.exceptions.NoSuchProfileException) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) ProfileException(org.craftercms.profile.api.exceptions.ProfileException) FileExistsException(org.apache.commons.io.FileExistsException)

Aggregations

FileInfo (org.craftercms.commons.mongo.FileInfo)2 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 FileExistsException (org.apache.commons.io.FileExistsException)1 ObjectId (org.bson.types.ObjectId)1 MongoDataException (org.craftercms.commons.mongo.MongoDataException)1 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)1 ProfileException (org.craftercms.profile.api.exceptions.ProfileException)1 ProfileAttachment (org.craftercms.profile.api.services.ProfileAttachment)1 NoSuchProfileException (org.craftercms.profile.exceptions.NoSuchProfileException)1