Search in sources :

Example 1 with InternalServerErrorException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.InternalServerErrorException in project FP-PSP-SERVER by FundacionParaguaya.

the class ImageParser method parse.

public static ImageDTO parse(String fileString, String imageDirectory) {
    ImageDTO image = null;
    // Image format validation (MIME type: image/jpeg, image/png, image/bmp, ...)
    String data = fileString.substring(0, fileString.indexOf(';'));
    String attribute = data.substring(0, data.indexOf(':'));
    if ("data".equals(attribute)) {
        String contentType = data.substring(data.indexOf(':') + 1);
        String type = contentType.substring(0, contentType.indexOf('/'));
        if ("image".equals(type)) {
            String format = contentType.substring(contentType.indexOf('/') + 1);
            String base64 = fileString.substring(fileString.indexOf(',') + 1);
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] fileBytes = decoder.decode(base64);
            File file;
            try {
                file = File.createTempFile("file", ".tmp");
                Files.write(fileBytes, file);
            } catch (IOException e) {
                LOG.error(e.getMessage(), e);
                throw new InternalServerErrorException(e);
            }
            image = new ImageDTO();
            image.setFile(file);
            image.setFormat(format);
            image.setImageDirectory(imageDirectory);
        }
    }
    return image;
}
Also used : Base64(java.util.Base64) InternalServerErrorException(py.org.fundacionparaguaya.pspserver.common.exceptions.InternalServerErrorException) IOException(java.io.IOException) MultipartFile(org.springframework.web.multipart.MultipartFile) File(java.io.File)

Example 2 with InternalServerErrorException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.InternalServerErrorException in project FP-PSP-SERVER by FundacionParaguaya.

the class TermCondPolServiceImpl method saveTerms.

@Override
public TermCondPolDTO saveTerms(MultipartFile htmlFile, TermCondPolDTO termCondPolDTO) {
    try {
        String htmlContent = new String(htmlFile.getBytes(), "UTF-8");
        TermCondPolEntity entity = mapper.dtoToEntity(termCondPolDTO);
        entity.setHtml(htmlContent);
        return mapper.entityToDto(repository.save(entity));
    } catch (IOException e) {
        throw new InternalServerErrorException(e);
    }
}
Also used : InternalServerErrorException(py.org.fundacionparaguaya.pspserver.common.exceptions.InternalServerErrorException) IOException(java.io.IOException) TermCondPolEntity(py.org.fundacionparaguaya.pspserver.security.entities.TermCondPolEntity)

Aggregations

IOException (java.io.IOException)2 InternalServerErrorException (py.org.fundacionparaguaya.pspserver.common.exceptions.InternalServerErrorException)2 File (java.io.File)1 Base64 (java.util.Base64)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1 TermCondPolEntity (py.org.fundacionparaguaya.pspserver.security.entities.TermCondPolEntity)1