Skip to content

Commit

Permalink
support directory downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
jpillora committed Oct 11, 2017
1 parent 6a050c2 commit feeaa1c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
23 changes: 17 additions & 6 deletions server/server_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"path/filepath"
"strings"
"time"

"github.com/jpillora/archive"
)

const fileNumberLimit = 1000
Expand Down Expand Up @@ -50,13 +52,22 @@ func (s *Server) serveFiles(w http.ResponseWriter, r *http.Request) {
}
switch r.Method {
case "GET":
f, err := os.Open(file)
if err != nil {
http.Error(w, "File open error: "+err.Error(), http.StatusBadRequest)
return
if info.IsDir() {
w.Header().Set("Content-Type", "application/zip")
w.WriteHeader(200)
//write .zip archive directly into response
a := archive.NewZipWriter(w)
a.AddDir(file)
a.Close()
} else {
f, err := os.Open(file)
if err != nil {
http.Error(w, "File open error: "+err.Error(), http.StatusBadRequest)
return
}
http.ServeContent(w, r, info.Name(), info.ModTime(), f)
f.Close()
}
http.ServeContent(w, r, info.Name(), info.ModTime(), f)
f.Close()
case "DELETE":
if err := os.RemoveAll(file); err != nil {
http.Error(w, "Delete failed: "+err.Error(), http.StatusInternalServerError)
Expand Down
5 changes: 2 additions & 3 deletions static/files/template/download-tree.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<!-- <div class="icon-container"> -->
<i class="wrapper icon">
<i ng-if="isdir()" ng-class="icon()" ng-click="toggle()"></i>
<i ng-if="isfile()" ng-class="icon()"></i>
<i ng-if="isfile()" ng-class="icon()"></i>
</i>
<!-- </div> -->

<div class="content">
<div class="header">
<span ng-if="isdir()">{{ node.Name }}</span>
<a ng-if="isfile() && !isdownloading()" ng-href="download/{{ node.$path }}">{{ node.Name }}</a>
<a ng-if="!isdownloading()" ng-href="download/{{ node.$path }}">{{ node.Name }}</a>
<span ng-if="isfile() && isdownloading()">{{ node.Name }}</span>
<span ng-if="!isdownloading()" class="controls">
<i ng-show="!confirm" ng-click="preremove()" class="red trash icon"></i>
Expand Down

0 comments on commit feeaa1c

Please sign in to comment.