Skip to content

Commit 1e32d30

Browse files
committed
Cleaner way of saving a multipart file to a temporarily location.
1 parent 7d715b9 commit 1e32d30

File tree

2 files changed

+15
-39
lines changed

2 files changed

+15
-39
lines changed

grails-app/controllers/com/wizpanda/file/UploadController.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class UploadController {
1717
FileUploadService fileUploadService
1818

1919
/**
20-
* This endpoint accepts the file in binary form, uploaded by the AJAX based application.
20+
* This endpoint accepts the file in the binary form, uploaded by the AJAX based application. This endpoint saves
21+
* the file in the temporary directory of the local server which will be cleaned automatically by the system itself.
2122
*
2223
* @example
2324
*
@@ -44,15 +45,15 @@ class UploadController {
4445
* This is done because if you install this plugin and forget to protect this endpoint then someone may bloat
4546
* your server by uploading junk files.
4647
*/
47-
def index() {
48+
def temporarily() {
4849
if (!Holders.getFlatConfig()["wizpanda.plugins.kernel.allow.file.upload"]) {
4950
log.warn "App is not allowing to upload files"
5051
return
5152
}
5253

5354
log.debug "Temporary upload with $params"
5455

55-
File file = fileUploadService.saveUploadedFile(params.file)
56+
File file = fileUploadService.saveTemporarily(params.file)
5657
Map result = [filepath: file.getPath(), filename: file.getName()]
5758

5859
// Work around for IE

grails-app/services/com/wizpanda/file/FileUploadService.groovy

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,38 @@ class FileUploadService {
1010
MessageSource messageSource
1111

1212
/**
13-
* Save the uploaded multipart file to a temporary location.
13+
* Save the uploaded multipart file in the temporary directory of the local server which will be cleaned
14+
* automatically by the system itself.
1415
*
1516
* @param multipartFile
16-
* @return Saved file with a metaclass method "getOriginalFilename" to get the actual uploaded file name
17+
* @return Saved file in a temporary location
1718
* @throws FileNotFoundException
1819
* @throws IOException
1920
*/
20-
File saveUploadedFile(CommonsMultipartFile multipartFile) throws FileNotFoundException, IOException {
21+
File saveTemporarily(CommonsMultipartFile multipartFile) throws FileNotFoundException, IOException {
2122
if (!multipartFile || multipartFile.isEmpty()) {
2223
log.debug "Received file is either empty or does not exists"
24+
2325
throw new FileNotFoundException(messageSource.getMessage("kernel.uploaded.file.empty", null, null))
2426
}
2527

26-
27-
InputStream inputStream
28-
FileOutputStream fileOutputStream
29-
30-
byte[] fileRead = new byte[1024]
31-
3228
try {
3329
String originalFilename = multipartFile.getOriginalFilename()
34-
35-
log.debug "Uploaded file's name [$originalFilename]"
36-
37-
// Remove special characters other than "a-z" "A-Z" "0-9" "." "-" or "_"
38-
String fileName = originalFilename.replaceAll("[^a-zA-Z0-9//._-]+", "").toLowerCase()
39-
4030
File temporaryDirectory = Files.createTempDirectory(null).toFile()
41-
File temporaryFile = new File(temporaryDirectory, fileName)
42-
43-
log.debug "File [$fileName] will be saved in [${temporaryDirectory.absolutePath}]"
44-
45-
inputStream = multipartFile.getInputStream()
46-
fileOutputStream = new FileOutputStream(temporaryFile)
4731

48-
int i = inputStream.read(fileRead)
32+
log.debug "Uploaded file [$originalFilename] will be saved in [${temporaryDirectory.absolutePath}]"
4933

50-
while (i != -1) {
51-
fileOutputStream.write(fileRead, 0, i)
52-
i = inputStream.read(fileRead)
53-
}
54-
55-
// A dynamic method to get the original name of the file which was received
56-
temporaryFile.metaClass.getOriginalFilename = { ->
57-
return originalFilename
58-
}
34+
File temporaryFile = new File(temporaryDirectory, originalFilename)
35+
multipartFile.transferTo(temporaryFile)
5936

6037
return temporaryFile
61-
} catch (FileNotFoundException e) {
38+
} catch (IllegalStateException e) {
6239
log.error "Problem saving file", e
63-
throw e
40+
throw new FileNotFoundException(e.message)
41+
6442
} catch (IOException e) {
6543
log.error "Exception saving file", e
6644
throw e
67-
} finally {
68-
fileOutputStream?.close()
69-
inputStream?.close()
7045
}
7146
}
7247
}

0 commit comments

Comments
 (0)