Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.80 KB | None | 0 0
  1. {
  2.  
  3.  
  4.         String filename = request.getHeader("X-File-Name");
  5.         if (filename) {
  6.             def webRootDir = servletContext.getRealPath("/")
  7.             def userDir = new File(webRootDir, "/zalaczniki/${params.id}")
  8.             userDir.mkdirs()
  9.             String hashCode= System.currentTimeMillis()
  10.  
  11.             def is = request.getInputStream()
  12.             def file = new File(userDir, hashCode + "_" + filename)
  13.             def fos = new FileOutputStream(file)
  14.             IOUtils.copy(is, fos)
  15.             is.close()
  16.             fos.close()
  17.        
  18. Attachement attachement = new Attachement(user: session.user.toString(), fileName: file.name, fileHash: hashCode, fileSize: file.length(), fileType: new MimetypesFileTypeMap().getContentType(file))
  19.             try {
  20.         ServiceAction.withTransaction { status ->
  21.             ServiceAction action = ServiceAction.get(Long.parseLong(params.param1))
  22.                
  23.                     action.addToFiles(attachement)
  24.                     if (action.save(flush: true)) {
  25.                         render "{'success': 'true'}"
  26.                     }
  27.         }
  28.             } catch (StaleObjectStateException e) {
  29.                 log.debug 'alojz'
  30.                 boolean success = false
  31.                 for (int i = 0; i < 10; i++) {
  32.                     try {
  33.         ServiceAction.withTransaction { status ->
  34.             ServiceAction action = ServiceAction.get(Long.parseLong(params.param1))
  35.                
  36.                     action.addToFiles(attachement)
  37.                     action.save(flush: true)
  38.         }
  39.                  
  40.                         success = true
  41.                         break;
  42.                     } catch (StaleObjectStateException e2) {}
  43.                 }
  44.                 if (!success) throw e
  45.                 else render "{'success': 'true'}"
  46.             }
  47.         }
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement