Skip to content

Instantly share code, notes, and snippets.

@marshall
Created October 19, 2009 07:31
Show Gist options
  • Save marshall/213185 to your computer and use it in GitHub Desktop.
Save marshall/213185 to your computer and use it in GitHub Desktop.
<html>
<head>
<style>
body { font-family: Monaco; }
#drop_target {
-webkit-user-drop: element;
border-bottom: 2px dashed black;
background-color: #303030;
color: #909090;
font-size: 16px;
text-align: center;
width: 99%; height: 99%;
}
.file {
-webkit-user-drag: element;
background-color: #609060;
color: white;
font-size: 18px;
text-align: center;
font-weight: bold;
display: none;
float: center;
margin: 5px;
}
.sha1 {
font-size: 12px;
}
</style>
</head>
<body>
<div id="drop_target">Drop Files Here</div>
<script type="text/javascript">
function fileURLToPath(fileURL)
{
var path = fileURL.replace("file://localhost","")
.replace("file:///","");
if (Titanium.platform == "win32") {
path = path.replace(/\//g, "\\");
}
return path;
}
function getFileName(fileURL)
{
var path = fileURLToPath(fileURL);
var tokens = path.split("/");
return tokens[tokens.length-1];
}
function getSHA1(fileURL)
{
var path = fileURLToPath(fileURL);
var file = Titanium.Filesystem.getFile(path);
var blob = file.read()
return Titanium.Codec.digestToHex(Titanium.Codec.SHA1, blob);
}
function addSHA1DragListener(element, sha1, fileName)
{
element.addEventListener("dragstart", function(event) {
var sha1File = Titanium.Filesystem.getFile(
Titanium.Filesystem.createTempDirectory(), fileName + ".sha1.txt");
sha1File.write(sha1);
event.dataTransfer.setData("text/uri-list", "file://"+sha1File.nativePath());
});
}
var target = document.getElementById("drop_target");
target.addEventListener("dragenter", function(event) {
event.preventDefault();
return true;
});
target.addEventListener("dragover", function(event) {
event.preventDefault();
return true;
});
target.addEventListener("drop", function(event) {
var fileURLs = event.dataTransfer.getData("text/uri-list").split(/[\r\n]+/);
for (var i = 0; i < fileURLs.length; i++)
{
var fileURL = fileURLs[i];
var fileName = getFileName(fileURL);
var fileSHA1 = getSHA1(fileURL);
var div = $("<div />").addClass("file").html(fileName + ": ");
var sha1 = $("<span />").addClass("sha1").html(fileSHA1);
div.append(sha1);
addSHA1DragListener(div[0], fileSHA1, fileName);
$("#drop_target").append(div);
div.fadeIn();
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment