SHARE
TWEET

Untitled

a guest Jun 17th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Service
  2. public class UploadService {
  3.  
  4.     @Inject
  5.     @Qualifier("session")
  6.     Session session;
  7.  
  8.     @Inject
  9.     AsyncImageConvertService asyncImageConvertService;
  10.  
  11.     @Value("${aws.bucket-name}")
  12.     String awsBucketName;
  13.  
  14.     @Inject
  15.     AmazonS3 amazonS3;
  16.  
  17.     @Inject
  18.     ImageMapper imageMapper;
  19.  
  20.     @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
  21.     public void uploadImage(Image image, String url, Runnable fnSaveObject) {
  22.         File file = new File(System.getProperty("java.io.tmpdir"), image.getUrl());
  23.  
  24.         image.setUrl(url);
  25.         image.setCreator(session.getUser());
  26.         PutObjectRequest request = new PutObjectRequest(awsBucketName, image.getUrl(), file);
  27.         request.setCannedAcl(CannedAccessControlList.PublicRead);
  28.         amazonS3.putObject(request);
  29.  
  30.         TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
  31.             public void afterCommit() {
  32.                 asyncImageConvertService.processImage(image.getId(), file);
  33.             }
  34.         });
  35.  
  36.         imageMapper.insert(image);
  37.         fnSaveObject.run();
  38.     }
  39. }
  40.      
  41. uploadService.uploadImage(
  42.                 profileImage,
  43.                 "/user/" + user.getId() + "/" + profileImage.getUrl(),
  44.                 () -> userMapper.insertImage(user, profileImage)
  45. );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top