Skip to content

Instantly share code, notes, and snippets.

@itsgokhanyilmaz
Created August 29, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsgokhanyilmaz/9cb6945f01ba4bfa4053fb04a25e63f9 to your computer and use it in GitHub Desktop.
Save itsgokhanyilmaz/9cb6945f01ba4bfa4053fb04a25e63f9 to your computer and use it in GitHub Desktop.
BlobStorageServiceImpl
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(value = "mock.azure.blobstorage.service.enabled", havingValue = "false", matchIfMissing = true)
public class BlobStorageServiceImpl implements BlobStorageService {
private final Logger LOGGER = LoggerFactory.getLogger(BlobStorageServiceImpl.class);
private final CloudBlobContainer cloudBlobContainer;
@Override
public URI uploadPicture(MultipartFile multipartFile) {
URI uri;
String multipartName = multipartFile.getName().replaceAll("[\n|\r|\t]", "_");
LOGGER.info("Profile image uploading, image name: {}", multipartName);
try {
String extension = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
String fileName = String.join(".", UUID.randomUUID().toString(), extension);
CloudBlockBlob blob = cloudBlobContainer.getBlockBlobReference(fileName);
blob.upload(multipartFile.getInputStream(), -1);
uri = blob.getUri();
} catch (URISyntaxException | StorageException | IOException e) {
throw new UploadPictureFailedException();
}
return Optional.ofNullable(uri).orElseThrow(UploadPictureFailedException::new);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment