Created
September 5, 2013 02:59
-
-
Save PabloWestphalen/6445538 to your computer and use it in GitHub Desktop.
Jin's File Explorer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jin.explorer.servlets; | |
import java.io.File; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLDecoder; | |
import java.text.DecimalFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.List; | |
import javax.servlet.http.HttpServletRequest; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
@Controller | |
public class FileBrowser { | |
@RequestMapping(value = "/browse/**", method = RequestMethod.GET) | |
@ResponseBody | |
public String printWelcome(HttpServletRequest request) { | |
try { | |
File file = new File(getPathFromURI(request.getRequestURI())); | |
if (file.exists()) { | |
HashMap<String, Object> respMap = getFileInfo(file); | |
if (file.isDirectory()) { | |
respMap.put("files", getFilesMap(file)); | |
} | |
return new ObjectMapper().writeValueAsString(respMap); | |
} | |
} catch (Exception e) { | |
return "Invalid url"; | |
} | |
return null; | |
} | |
public String getPathFromURI(String uri) { | |
String[] path_frags = null; | |
try { | |
path_frags = URLDecoder.decode(uri, "UTF-8").split("/"); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
String device_letter = path_frags[3]; | |
String full_path = ""; | |
for (int i = 4; i < path_frags.length; i++) { | |
full_path += path_frags[i]; | |
if (i + 1 < path_frags.length) { | |
full_path += "/"; | |
} | |
} | |
return device_letter + ":/" + full_path; | |
} | |
public HashMap<String, Object> getFileInfo(File file) { | |
HashMap<String, Object> respMap = new HashMap<String, Object>(); | |
respMap.put("exists", file.exists()); | |
respMap.put("isFile", file.isFile()); | |
respMap.put("winPath", file.getAbsolutePath()); | |
respMap.put("path", getFilePath(file.getAbsolutePath())); | |
respMap.put("name", file.getName()); | |
respMap.put("size", getFileSize(file)); | |
respMap.put("lastModified", getLastModified(file)); | |
respMap.put("type", getFileType(file)); | |
respMap.put("isHidden", file.isHidden()); | |
return respMap; | |
} | |
public List<HashMap<String, Object>> getFilesMap(File dir) { | |
List<HashMap<String, Object>> filesMap = new ArrayList<HashMap<String, Object>>(); | |
for (File f : dir.listFiles()) { | |
HashMap<String, Object> fileMap = getFileInfo(f); | |
filesMap.add(fileMap); | |
} | |
return filesMap; | |
} | |
public String getFileSize(File f) { | |
if(f.isDirectory()) return ""; | |
long size = f.length(); | |
if (size == 0) | |
return "0"; | |
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" }; | |
int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); | |
return new DecimalFormat("#,##0.#").format(size | |
/ Math.pow(1024, digitGroups)) | |
+ " " + units[digitGroups]; | |
} | |
public String getLastModified(File f) { | |
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYYY hh:mm"); | |
return sdf.format(new Date(f.lastModified())); | |
} | |
public String getFileType(File f) { | |
if (f.isDirectory()) | |
return "directory"; | |
String name = f.getName(); | |
String extension = name.substring(name.lastIndexOf('.') + 1, | |
name.length()); | |
String soundPattern = "mp3|wav|flac"; | |
String videoPattern = "avi|mkv|mp4|3gp"; | |
String imagePattern = "jpg|jpeg|png|gif"; | |
if (extension.matches(soundPattern)) | |
return "sound"; | |
if (extension.matches(videoPattern)) | |
return "video"; | |
if (extension.matches(imagePattern)) | |
return "image"; | |
return "file"; | |
} | |
public String getFilePath(String path) { | |
return path.replaceAll("\\\\", "/").replaceFirst(":", ""); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Jin's File Explorer</title> | |
<spring:url var="rsrc" value="/static" /> | |
<meta name="viewport" content="width=device-width"> | |
<link rel="stylesheet" href="${rsrc}/css/main.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<table id="files" class="tablesorter"> | |
<thead> | |
<tr> | |
<th>Name</th> | |
<th>Date modified</th> | |
<th>Type</th> | |
<th>Size</th> | |
<th>Misc</th> | |
</tr> | |
</thead> | |
<tbody> | |
<script id="filesTpl" type="text/template"> | |
{{#files}} | |
<tr id="{{@index}}"> | |
<td class="{{type}}"><a href="#">{{name}}</a></td> | |
<td>{{lastModified}}</td> | |
<td>{{type}}</td> | |
<td>{{size}}</td> | |
<td>{{isFile}}</td> | |
</tr> | |
{{/files}} | |
</script> | |
</tbody> | |
</table> | |
</div> | |
<script src="${rsrc}/js/vendor/jquery.min.js"></script> | |
<script src="${rsrc}/js/vendor/handlebars.js"></script> | |
<script src="${rsrc}/js/main.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj; | |
var template; | |
var binder; | |
$(document).ready(function() { | |
template = $('#filesTpl').html(); | |
binder = Handlebars.compile(template); | |
listDirectory('g'); | |
}); | |
function listDirectory(path) { | |
$.getJSON('browse/' + path, function(data) { | |
obj = data; | |
$('#files tbody').html(binder(data)); | |
$("#files").tablesorter(); | |
addClickHandlers(); | |
}); | |
} | |
function addClickHandlers() { | |
$("#files tbody tr").click(function(event) { | |
var file = obj.files[$(this).attr('id')]; | |
console.log(file); | |
listDirectory(file.path); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment