diff --git a/FileViewerGrailsPlugin.groovy b/FileViewerGrailsPlugin.groovy index 2f54eb4..bb704b0 100644 --- a/FileViewerGrailsPlugin.groovy +++ b/FileViewerGrailsPlugin.groovy @@ -15,7 +15,7 @@ class FileViewerGrailsPlugin { def author = "Himanshu Seth, Fabien Benichou" def authorEmail = "himanshu@intelligrape.com, fabien.benichou@gmail.com" - def title = "Plugin summary/headline" + def title = "File Viewer Grails plugin" def description = '''\\ File Viewer Grails plugin provides a user friendly way for viewing folders and files on the system. The most common use-case is to see the logs on the server. Documentation available at http://github.com/IntelliGrape/File-Viewer-Grails-Plugin/wiki @@ -25,11 +25,9 @@ Documentation available at http://github.com/IntelliGrape/File-Viewer-Grails-Plu def documentation = "http://grails.org/plugin/file-viewer" def doWithWebDescriptor = { xml -> - // TODO Implement additions to web.xml (optional), this event occurs before } def doWithSpring = { - // TODO Implement runtime spring config (optional) def config = application.config.grails.fileViewer fileLocations(org.grails.plugins.fileviewer.FileLocations) { locations = config.locations ?: [System.getProperty("java.io.tmpdir")] @@ -38,21 +36,17 @@ Documentation available at http://github.com/IntelliGrape/File-Viewer-Grails-Plu } def doWithDynamicMethods = { ctx -> - // TODO Implement registering dynamic methods to classes (optional) } def doWithApplicationContext = { applicationContext -> - // TODO Implement post initialization spring config (optional) } def onChange = { event -> - // TODO Implement code that is executed when any artefact that this plugin is // watching is modified and reloaded. The event contains: event.source, // event.application, event.manager, event.ctx, and event.plugin. } def onConfigChange = { event -> - // TODO Implement code that is executed when the project configuration changes. // The event is the same as for 'onChange'. } } diff --git a/grails-app/controllers/org/grails/plugins/fileviewer/FileController.groovy b/grails-app/controllers/org/grails/plugins/fileviewer/FileController.groovy index 567a438..764a508 100644 --- a/grails-app/controllers/org/grails/plugins/fileviewer/FileController.groovy +++ b/grails-app/controllers/org/grails/plugins/fileviewer/FileController.groovy @@ -1,33 +1,56 @@ package org.grails.plugins.fileviewer -import org.grails.plugins.fileviewer.FileViewerUtils +/** + * @author Himanshu Seth (himanshu@intelligrape.com) + * @author Fabien Benichou (fabien.benichou@gmail.com) + */ class FileController { def fileLocations def index = { - Map model = [locations:fileLocations.locations] + Map model = [locations: fileLocations.locations] if (params.filePath) { File file = new File(params.filePath) if (file.exists()) { - if ( file.isFile()) { + if (file.isFile()) { List locations = getSubFiles(file.parentFile) String fileContents = getFileContents(file) - model= [locations: locations, fileContents: fileContents, filePath: file.absolutePath] + model = [locations: locations, fileContents: fileContents, filePath: file.absolutePath] } else { List locations = getSubFiles(file) - model= [locations:locations] + model = [locations: locations] } - if(!fileLocations.locations.contains(file.absolutePath)){ + if (!fileLocations.locations.contains(file.absolutePath)) { model['prevLocation'] = file.getParentFile().absolutePath } model['showBackLink'] = true } } - render(view: "/file/fileList", model: model, plugin:'fileViewer') + 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 locations = [] file.eachFile {File subFile -> @@ -36,9 +59,15 @@ class FileController { return locations } - + /** + * getFileContents reads file line by line + * + * @param file + * + * @return file contets formatted by
html tag + */ private def getFileContents(File file) { - String fileContents; + String fileContents = ""; List contents = file.text.readLines() if (contents.size() > fileLocations.linesCount) { int startIndex = contents.size() - (fileLocations.linesCount + 1) @@ -49,17 +78,4 @@ class FileController { } return fileContents } - - 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() - } } diff --git a/grails-app/views/file/_fileDetails.gsp b/grails-app/views/file/_fileDetails.gsp index 32f3cfd..5538942 100644 --- a/grails-app/views/file/_fileDetails.gsp +++ b/grails-app/views/file/_fileDetails.gsp @@ -1,6 +1,8 @@
${fileContents}
\ No newline at end of file diff --git a/grails-app/views/file/fileList.gsp b/grails-app/views/file/fileList.gsp index e3c4737..2ed63f8 100644 --- a/grails-app/views/file/fileList.gsp +++ b/grails-app/views/file/fileList.gsp @@ -2,13 +2,17 @@ - File List + <g:message code="default.page.title.label" default="File List" /> -
Please click on the links below to view detailed reports:

+
+ +

diff --git a/grails-file-viewer-0.1.zip b/grails-file-viewer-0.1.zip index e928db4..e7670ea 100644 Binary files a/grails-file-viewer-0.1.zip and b/grails-file-viewer-0.1.zip differ diff --git a/plugin.xml b/plugin.xml index 1f18397..d54337f 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,8 +1,10 @@ - Your name - Plugin summary/headline + Himanshu Seth, Fabien Benichou + himanshu@intelligrape.com, fabien.benichou@gmail.com + File Viewer Grails plugin \ -Brief description of the plugin. +File Viewer Grails plugin provides a user friendly way for viewing folders and files on the system. The most common use-case is to see the logs on the server. +Documentation available at http://github.com/IntelliGrape/File-Viewer-Grails-Plugin/wiki http://grails.org/plugin/file-viewer diff --git a/src/groovy/org/grails/plugins/fileviewer/FileLocations.groovy b/src/groovy/org/grails/plugins/fileviewer/FileLocations.groovy index 969968a..86fbd8d 100644 --- a/src/groovy/org/grails/plugins/fileviewer/FileLocations.groovy +++ b/src/groovy/org/grails/plugins/fileviewer/FileLocations.groovy @@ -1,12 +1,10 @@ package org.grails.plugins.fileviewer /** - * Created by IntelliJ IDEA. - * User: himanshu - * Date: 7 Sep, 2010 - * Time: 6:24:59 PM - * To change this template use File | Settings | File Templates. + * @author Himanshu Seth (himanshu@intelligrape.com) + * @author Fabien Benichou (fabien.benichou@gmail.com) */ + class FileLocations { List locations Integer linesCount diff --git a/src/groovy/org/grails/plugins/fileviewer/FileViewerUtils.groovy b/src/groovy/org/grails/plugins/fileviewer/FileViewerUtils.groovy index 979431e..a46cb48 100644 --- a/src/groovy/org/grails/plugins/fileviewer/FileViewerUtils.groovy +++ b/src/groovy/org/grails/plugins/fileviewer/FileViewerUtils.groovy @@ -1,7 +1,18 @@ package org.grails.plugins.fileviewer -class FileViewerUtils { +/** + * @author Himanshu Seth (himanshu@intelligrape.com) + * @author Fabien Benichou (fabien.benichou@gmail.com) + */ +class FileViewerUtils { + /** + * getMimeContentType gets mime content type depending on the extension of file name + * + * @param extension e.g. 'asc' + * + * @return the mime content type e.g. 'text/plain' + */ public static String getMimeContentType(String extension) { Map mimeTypes = ['ai': 'application/postscript', 'aif': 'audio/x-aiff', @@ -176,12 +187,4 @@ class FileViewerUtils { 'zip': 'application/zip'] return mimeTypes.get(extension.toLowerCase()) } - - public static boolean isText(String fileName) { - String extension = (fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length())).toLowerCase() - String mimeType = getMimeContentType(extension) - return ((mimeType) && (mimeType?.startsWith("text"))) - } - - }