Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- String filename = request.getHeader("X-File-Name");
- if (filename) {
- def webRootDir = servletContext.getRealPath("/")
- def userDir = new File(webRootDir, "/zalaczniki/${params.id}")
- userDir.mkdirs()
- String hashCode= System.currentTimeMillis()
- def is = request.getInputStream()
- def file = new File(userDir, hashCode + "_" + filename)
- def fos = new FileOutputStream(file)
- IOUtils.copy(is, fos)
- is.close()
- fos.close()
- Attachement attachement = new Attachement(user: session.user.toString(), fileName: file.name, fileHash: hashCode, fileSize: file.length(), fileType: new MimetypesFileTypeMap().getContentType(file))
- try {
- ServiceAction.withTransaction { status ->
- ServiceAction action = ServiceAction.get(Long.parseLong(params.param1))
- action.addToFiles(attachement)
- if (action.save(flush: true)) {
- render "{'success': 'true'}"
- }
- }
- } catch (StaleObjectStateException e) {
- log.debug 'alojz'
- boolean success = false
- for (int i = 0; i < 10; i++) {
- try {
- ServiceAction.withTransaction { status ->
- ServiceAction action = ServiceAction.get(Long.parseLong(params.param1))
- action.addToFiles(attachement)
- action.save(flush: true)
- }
- success = true
- break;
- } catch (StaleObjectStateException e2) {}
- }
- if (!success) throw e
- else render "{'success': 'true'}"
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement