Created
August 29, 2020 14:37
-
-
Save itsgokhanyilmaz/9cb6945f01ba4bfa4053fb04a25e63f9 to your computer and use it in GitHub Desktop.
BlobStorageServiceImpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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