Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Service
- public class UploadService {
- @Inject
- @Qualifier("session")
- Session session;
- @Inject
- AsyncImageConvertService asyncImageConvertService;
- @Value("${aws.bucket-name}")
- String awsBucketName;
- @Inject
- AmazonS3 amazonS3;
- @Inject
- ImageMapper imageMapper;
- @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
- public void uploadImage(Image image, String url, Runnable fnSaveObject) {
- File file = new File(System.getProperty("java.io.tmpdir"), image.getUrl());
- image.setUrl(url);
- image.setCreator(session.getUser());
- PutObjectRequest request = new PutObjectRequest(awsBucketName, image.getUrl(), file);
- request.setCannedAcl(CannedAccessControlList.PublicRead);
- amazonS3.putObject(request);
- TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
- public void afterCommit() {
- asyncImageConvertService.processImage(image.getId(), file);
- }
- });
- imageMapper.insert(image);
- fnSaveObject.run();
- }
- }
- uploadService.uploadImage(
- profileImage,
- "/user/" + user.getId() + "/" + profileImage.getUrl(),
- () -> userMapper.insertImage(user, profileImage)
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement