upload and drag n drop upades

pull/144/head
Henrique Dias 2016-06-23 09:15:00 +01:00
parent 1e3ec14e92
commit d66bce3e6d
3 changed files with 88 additions and 81 deletions

View File

@ -259,69 +259,28 @@ var downloadEvent = function (event) {
return false; return false;
} }
var uploadEvent = function (event) { var handleFiles = function(files) {
event.preventDefault(); let button = document.getElementById("upload");
let button = document.getElementById("upload-input");
button.click();
button.addEventListener("change", (event) => {
event.preventDefault();
let html = button.changeToLoading(); let html = button.changeToLoading();
let files = event.target.files;
let data = new FormData(); let data = new FormData();
/* for (let i = 0; i < files.length; i++) {
event.preventDefault(); data.append(files[i].name, files[i]);
files = event.target.files; }
$('#loading').fadeIn(); let request = new XMLHttpRequest();
request.open('POST', window.location.pathname);
request.setRequestHeader("Upload", "true");
request.send(data);
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200) {
location.reload();
}
// Create a formdata object and add the files button.changeToDone((request.status != 200), html);
var data = new FormData(); }
$.each(files, function(key, value) { }
data.append(key, value);
});
$.ajax({
url: window.location.pathname,
type: 'POST',
data: data,
cache: false,
dataType: 'json',
headers: {
'X-Upload': 'true',
},
processData: false,
contentType: false,
}).done(function(data) {
notification({
text: "File(s) uploaded successfully.",
type: 'success',
timeout: 5000
});
$('#loading').fadeOut();
$.pjax({
url: window.location.pathname,
container: '#content'
})
}).fail(function(data) {
$('#loading').fadeOut();
notification({
text: 'Something went wrong.',
type: 'error'
});
console.log(data);
});
return false;
*/
});
return false; return false;
} }
@ -389,6 +348,8 @@ var localizeDatetime = function (e, index, ar) {
e.textContent = d.toLocaleString(); e.textContent = d.toLocaleString();
} }
document.addEventListener("DOMContentLoaded", function (event) { document.addEventListener("DOMContentLoaded", function (event) {
var timeList = Array.prototype.slice.call(document.getElementsByTagName("time")); var timeList = Array.prototype.slice.call(document.getElementsByTagName("time"));
timeList.forEach(localizeDatetime); timeList.forEach(localizeDatetime);
@ -407,24 +368,67 @@ document.addEventListener("DOMContentLoaded", function (event) {
document.getElementById("view").addEventListener("click", viewEvent) document.getElementById("view").addEventListener("click", viewEvent)
}; };
if (document.getElementById("upload")) { if (document.getElementById("upload")) {
document.getElementById("upload").addEventListener("click", uploadEvent); document.getElementById("upload").addEventListener("click", (event) => {
document.getElementById("upload-input").click();
});
} }
document.getElementById("delete").addEventListener("click", deleteEvent); document.getElementById("delete").addEventListener("click", deleteEvent);
document.getElementById("rename").addEventListener("click", renameEvent); document.getElementById("rename").addEventListener("click", renameEvent);
document.getElementById("download").addEventListener("click", downloadEvent); document.getElementById("download").addEventListener("click", downloadEvent);
var drop = document.getElementById("listing"); let drop = document.getElementById("listing");
drop.addEventListener("dragenter", change, false); /* if (drop) {
drop.addEventListener("dragleave",change_back,false); drop.addEventListener("dragenter", dragenter, false);
drop.addEventListener("dragover", dragover, false);
drop.addEventListener("dragdrop", drop, false);
} */
function change() { drop.addEventListener("drag", function( event ) {
console.log("hey")
drop.style.backgroundColor = '#000';
};
function change_back() { }, false);
drop.style.backgroundColor = 'transparent';
}; drop.addEventListener("dragstart", function( event ) {
// store a ref. on the dragged elem
dragged = event.target;
// make it half transparent
event.target.style.opacity = .5;
}, false);
drop.addEventListener("dragend", function( event ) {
// reset the transparency
event.target.style.opacity = "";
}, false);
drop.addEventListener("dragover", function( event ) {
event.preventDefault();
}, false);
drop.addEventListener("dragenter", function( event ) {
// highlight potential drop target when the draggable element enters it
event.target.style.background = "purple";
}, false);
drop.addEventListener("dragleave", function( event ) {
// reset background of potential drop target when the draggable element leaves it
if ( event.target.className == "dropzone" ) {
event.target.style.background = "";
}
}, false);
drop.addEventListener("drop", function( event ) {
// prevent default action (open as link for some elements)
event.preventDefault();
var dt = event.dataTransfer;
var files = dt.files;
handleFiles(files);
}, false);
return false; return false;
}); });

View File

@ -1,5 +1,5 @@
{{ define "content" }} {{ define "content" }}
<div class="listing"> <div class="listing" draggable="true">
<div class="container" id="listing"> <div class="container" id="listing">
{{- range .Items}} {{- range .Items}}
<div class="item" id="{{.URL}}"> <div class="item" id="{{.URL}}">
@ -30,5 +30,5 @@
</div> </div>
</div> </div>
<input style="display:none" type="file" id="upload-input" value="Upload" multiple> <input style="display:none" type="file" id="upload-input" onchange="handleFiles(this.files)" value="Upload" multiple>
{{ end }} {{ end }}

View File

@ -9,6 +9,7 @@ package filemanager
import ( import (
"io" "io"
"log"
"mime" "mime"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
@ -135,25 +136,27 @@ func Upload(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
// Parse the multipart form in the request // Parse the multipart form in the request
err := r.ParseMultipartForm(100000) err := r.ParseMultipartForm(100000)
if err != nil { if err != nil {
log.Println(err)
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
// For each file header in the multipart form // For each file header in the multipart form
for _, fheaders := range r.MultipartForm.File { for _, headers := range r.MultipartForm.File {
// Handle each file // Handle each file
for _, hdr := range fheaders { for _, header := range headers {
// Open the first file // Open the first file
var infile multipart.File var src multipart.File
if infile, err = hdr.Open(); nil != err { if src, err = header.Open(); nil != err {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
filename := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1) filename := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
filename = filename + header.Filename
filename = filepath.Clean(filename) filename = filepath.Clean(filename)
// Create the file // Create the file
var outfile *os.File var dst *os.File
if outfile, err = os.Create(filename); nil != err { if dst, err = os.Create(filename); nil != err {
if os.IsExist(err) { if os.IsExist(err) {
return http.StatusConflict, err return http.StatusConflict, err
} }
@ -161,11 +164,11 @@ func Upload(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
} }
// Copy the file content // Copy the file content
if _, err = io.Copy(outfile, infile); nil != err { if _, err = io.Copy(dst, src); nil != err {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
defer outfile.Close() defer dst.Close()
} }
} }