-
Notifications
You must be signed in to change notification settings - Fork 3
/
FileController.groovy
81 lines (74 loc) · 2.16 KB
/
FileController.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.grails.plugins.fileviewer
/**
* @author Himanshu Seth (himanshu@intelligrape.com)
* @author Fabien Benichou (fabien.benichou@gmail.com)
*/
class FileController {
def fileLocations
def index = {
Map model = [locations: fileLocations.locations]
if (params.filePath) {
File file = new File(params.filePath)
if (file.exists()) {
if (file.isFile()) {
List locations = getSubFiles(file.parentFile)
String fileContents = getFileContents(file)
model = [locations: locations, fileContents: fileContents, filePath: file.absolutePath]
} else {
List locations = getSubFiles(file)
model = [locations: locations]
}
if (!fileLocations.locations.contains(file.absolutePath)) {
model['prevLocation'] = file.getParentFile().absolutePath
}
model['showBackLink'] = true
}
}
render(view: "/file/fileList", model: model, plugin: 'fileViewer')
}
def downloadFile = {
File file = new File(params.filePath)
byte[] assetContent = file.readBytes();
response.setContentLength(assetContent.size())
response.setHeader("Content-disposition", "attachment; filename=${file.name}")
String contentType = FileViewerUtils.getMimeContentType(file.name.tokenize(".").last().toString())
response.setContentType(contentType)
OutputStream out = response.getOutputStream()
out.write(assetContent)
out.flush()
out.close()
}
/**
* getSubFiles gets list of subfiles
*
* @param file
*
* @return List
*/
private List getSubFiles(File file) {
List<String> locations = []
file.eachFile {File subFile ->
locations << subFile.absolutePath
}
return locations
}
/**
* getFileContents reads file line by line
*
* @param file
*
* @return file contets formatted by <br/> html tag
*/
private def getFileContents(File file) {
String fileContents = "";
List<String> contents = file.text.readLines()
if (contents.size() > fileLocations.linesCount) {
int startIndex = contents.size() - (fileLocations.linesCount + 1)
int endIndex = contents.size() - 1
fileContents = contents.subList(startIndex, endIndex).join("<br/>")
} else {
fileContents = contents.join("<br/>")
}
return fileContents
}
}