Search in sources :

Example 1 with ProfileAttachment

use of org.craftercms.profile.api.services.ProfileAttachment in project profile by craftercms.

the class ProfileServiceImpl method fileInfoToProfileAttachment.

private ProfileAttachment fileInfoToProfileAttachment(final FileInfo fileInfo) {
    if (fileInfo == null) {
        return new ProfileAttachment();
    }
    ProfileAttachment attachment = new ProfileAttachment();
    attachment.setContentType(fileInfo.getContentType());
    ;
    attachment.setMd5(fileInfo.getMd5());
    attachment.setFileName(fileInfo.getStoreName().substring(fileInfo.getStoreName().lastIndexOf("/") + 1));
    attachment.setFileSize(fileInfo.getFileSize());
    attachment.setFileSizeBytes(fileInfo.getFileSizeBytes());
    attachment.setId(fileInfo.getFileId().toString());
    return attachment;
}
Also used : ProfileAttachment(org.craftercms.profile.api.services.ProfileAttachment)

Example 2 with ProfileAttachment

use of org.craftercms.profile.api.services.ProfileAttachment 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 3 with ProfileAttachment

use of org.craftercms.profile.api.services.ProfileAttachment in project profile by craftercms.

the class ProfileController method getAttachment.

@RequestMapping(value = URL_PROFILE_GET_ATTACHMENT, method = RequestMethod.GET)
@ApiOperation(value = "Gets the requested attachment of the given profile", notes = "If Attachment or profile does not exist will " + "throw error, content-type,content-legnth headers" + " are set")
public void getAttachment(@ApiParam("The profile's ID") @PathVariable(PATH_VAR_ID) String profileId, @ApiParam("Attachment Id to get") @PathVariable(PATH_VAR_ATTACHMENT) String attachmentId, HttpServletResponse response) throws ProfileException, IOException {
    Profile profile = profileService.getProfile(profileId);
    if (profile != null) {
        InputStream input = null;
        try {
            input = profileService.getProfileAttachment(attachmentId, profile.getId().toString());
            if (input != null) {
                ProfileAttachment attachment = profileService.getProfileAttachmentInformation(profile.getId().toString(), attachmentId);
                response.setContentType(attachment.getContentType());
                response.setContentLength((int) attachment.getFileSizeBytes());
                IOUtils.copy(input, response.getOutputStream());
            }
        } catch (ProfileException ex) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            response.setContentLength(0);
        } finally {
            if (input != null) {
                input.close();
            }
        }
    } else {
        throw new NoSuchProfileException.ById(profileId);
    }
}
Also used : ProfileAttachment(org.craftercms.profile.api.services.ProfileAttachment) InputStream(java.io.InputStream) NoSuchProfileException(org.craftercms.profile.exceptions.NoSuchProfileException) ProfileException(org.craftercms.profile.api.exceptions.ProfileException) Profile(org.craftercms.profile.api.Profile) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ProfileAttachment (org.craftercms.profile.api.services.ProfileAttachment)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 FileInfo (org.craftercms.commons.mongo.FileInfo)1 Profile (org.craftercms.profile.api.Profile)1 ProfileException (org.craftercms.profile.api.exceptions.ProfileException)1 NoSuchProfileException (org.craftercms.profile.exceptions.NoSuchProfileException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1