Skip to content

Commit

Permalink
added java doc and removed unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhagwat Kumar committed Sep 25, 2010
1 parent d2293cd commit 32c42a7
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 50 deletions.
8 changes: 1 addition & 7 deletions FileViewerGrailsPlugin.groovy
Expand Up @@ -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
Expand All @@ -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")]
Expand All @@ -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'.
}
}
@@ -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<String> locations = []
file.eachFile {File subFile ->
Expand All @@ -36,9 +59,15 @@ class FileController {
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;
String fileContents = "";
List<String> contents = file.text.readLines()
if (contents.size() > fileLocations.linesCount) {
int startIndex = contents.size() - (fileLocations.linesCount + 1)
Expand All @@ -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()
}
}
4 changes: 3 additions & 1 deletion grails-app/views/file/_fileDetails.gsp
@@ -1,6 +1,8 @@
<g:if test="${filePath}">
<div class="fileContents">${fileContents}</div>
<div class="downloadLink">
<a href="${createLink(controller:'file', action:'downloadFile', params:[filePath:filePath])}">Download complete file</a>
<a href="${createLink(controller:'file', action:'downloadFile', params:[filePath:filePath])}">
<g:message code="default.link.download.label" default="Download complete file" />
</a>
</div>
</g:if>
10 changes: 7 additions & 3 deletions grails-app/views/file/fileList.gsp
Expand Up @@ -2,13 +2,17 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="none"/>
<title>File List</title>
<title><g:message code="default.page.title.label" default="File List" /></title>
</head>
<body>
<br/><strong>Please click on the links below to view detailed reports:</strong><br/><br/>
<br/><strong>
<g:message code="default.page.body.detail" default="Please click on the links below to view detailed reports:" />
</strong><br/><br/>
<g:if test="${showBackLink}">
<div id="backLink">
<a class="showReportLink" href="${createLink(action: 'index', params: [filePath: prevLocation])}">Back</a>
<a class="showReportLink" href="${createLink(action: 'index', params: [filePath: prevLocation])}">
<g:message code="default.link.back.label" default="Back" />
</a>
</div>
</g:if>
<div id="mainContainer">
Expand Down
Binary file modified grails-file-viewer-0.1.zip
Binary file not shown.
8 changes: 5 additions & 3 deletions plugin.xml
@@ -1,8 +1,10 @@
<plugin name='file-viewer' version='0.1' grailsVersion='1.3.3 &gt; *'>
<author>Your name</author>
<title>Plugin summary/headline</title>
<author>Himanshu Seth, Fabien Benichou</author>
<authorEmail>himanshu@intelligrape.com, fabien.benichou@gmail.com</authorEmail>
<title>File Viewer Grails plugin</title>
<description>\
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
</description>
<documentation>http://grails.org/plugin/file-viewer</documentation>
<resources>
Expand Down
8 changes: 3 additions & 5 deletions 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<String> locations
Integer linesCount
Expand Down
21 changes: 12 additions & 9 deletions 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<String, String> mimeTypes = ['ai': 'application/postscript',
'aif': 'audio/x-aiff',
Expand Down Expand Up @@ -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")))
}


}

0 comments on commit 32c42a7

Please sign in to comment.