Browse Source

添加删除接口

tangs 6 years ago
parent
commit
1325a849ab
3 changed files with 34 additions and 3 deletions
  1. 1 0
      config.json
  2. 32 3
      src/fs/fs.go
  3. 1 0
      src/fs/router.go

+ 1 - 0
config.json

@@ -1,5 +1,6 @@
 {
     "filePath": "/home/tangs/file",
+    "deletePath": "/home/tangs/deleteFiles",
     "log": "/var/log/fileUpload/log.log",
     "addr": "http://file.5tangs.com/"
 }

+ 32 - 3
src/fs/fs.go

@@ -8,12 +8,15 @@ import (
 	"os"
 	"io"
 	"encoding/json"
+	"os/exec"
+	"strings"
 )
 
 type Config struct {
-	FilePath string `json:"filePath"`
-	Log      string `json:"log"`
-	Addr     string `json:"addr"`
+	FilePath   string `json:"filePath"`
+	DeletePath string `json:"deletePath"`
+	Log        string `json:"log"`
+	Addr       string `json:"addr"`
 }
 
 var Conf = &Config{}
@@ -80,4 +83,30 @@ func FileUploadHandle(w http.ResponseWriter, r *http.Request) {
 	log.Debug("FileUploadHandle copy file (%v) success with length(%v)", filename, length)
 	code = 0
 	msg = "Ok"
+}
+
+func DeleteFile(w http.ResponseWriter, r *http.Request) {
+	var name = r.FormValue("name")
+	if len(name) < 1 {
+		w.WriteHeader(404)
+		w.Write([]byte("name is empty"))
+		log.Error("DeleteFile name is empty")
+		return
+	}
+	
+	var deleteFile= Conf.DeletePath
+	if !strings.HasSuffix(deleteFile, "/") {
+		deleteFile += "/"
+	}
+	cmd := exec.Command("mv", name, Conf.DeletePath)
+	output,err :=cmd.CombinedOutput()
+	if err != nil {
+		w.Write([]byte(err.Error()))
+		log.Error("DeleteFile command with name: %v, output: %v error: %v", name, deleteFile, err)
+		return
+	}
+	
+	log.Debug("DeleteFile command with name: %v, output: %v success with output: %v", name, deleteFile, string(output))
+	w.Write([]byte("Success"))
+	return 
 }

+ 1 - 0
src/fs/router.go

@@ -4,4 +4,5 @@ import "net/http"
 
 func Router() {
 	http.HandleFunc("/uploadFile", FileUploadHandle)
+	http.HandleFunc("/delteFile", DeleteFile)
 }