Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
create dir when push to a not exists directory
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Nov 20, 2017
1 parent 9597d56 commit 02858c5
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"os/user"
"path/filepath"

"flag"
"fmt"
Expand Down Expand Up @@ -480,10 +481,10 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {
io.WriteString(w, "Success")
})

m.HandleFunc("/upload/{filepath:.*}", func(w http.ResponseWriter, r *http.Request) {
filepath := mux.Vars(r)["filepath"]
m.HandleFunc("/upload/{target:.*}", func(w http.ResponseWriter, r *http.Request) {
target := mux.Vars(r)["target"]
if runtime.GOOS != "windows" {
filepath = "/" + filepath
target = "/" + target
}
var fileMode os.FileMode
if _, err := fmt.Sscanf(r.FormValue("mode"), "%o", &fileMode); err != nil {
Expand All @@ -500,29 +501,35 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {
file.Close()
r.MultipartForm.RemoveAll()
}()
if strings.HasSuffix(filepath, "/") {
filepath = path.Join(filepath, header.Filename)
if strings.HasSuffix(target, "/") {
target = path.Join(target, header.Filename)
}

targetDir := filepath.Dir(target)
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
os.MkdirAll(targetDir, 0755)
}
target, err := os.Create(filepath)

fd, err := os.Create(target)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer target.Close()
written, err := io.Copy(target, file)
defer fd.Close()
written, err := io.Copy(fd, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if fileMode != 0 {
os.Chmod(filepath, fileMode)
os.Chmod(target, fileMode)
}
if fileInfo, err := os.Stat(filepath); err == nil {
if fileInfo, err := os.Stat(target); err == nil {
fileMode = fileInfo.Mode()
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(map[string]interface{}{
"target": filepath,
"target": target,
"size": written,
"mode": fmt.Sprintf("0%o", fileMode),
})
Expand Down

0 comments on commit 02858c5

Please sign in to comment.