-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.go
130 lines (122 loc) · 2.9 KB
/
operation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
kfs "github.com/kiyor/k2fs/lib"
)
// Operation api request
type Operation struct {
Files map[string]bool `json:"files"`
Dir string `json:"dir"`
Action string `json:"action"`
}
var Trash string
func apiOperation(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var op Operation
err := json.NewDecoder(r.Body).Decode(&op)
if err != nil {
NewErrResp(w, 1, err)
return
}
// log.Println(toJSON(op))
path := filepath.Join(rootDir, op.Dir)
meta := kfs.NewMeta(path)
// log.Println(toJSON(meta))
for k, b := range op.Files {
file := filepath.Join(path, k)
if b {
m, _ := meta.Get(k)
switch {
case op.Action == "unzip":
var cmd string
var c *exec.Cmd
d := filepath.Dir(file)
f := filepath.Base(file)
b := filepath.Join(d, f[:len(f)-4])
err := os.Mkdir(b, 0755)
if err != nil {
log.Println(err)
return
}
switch filepath.Ext(strings.ToLower(file)) {
case ".rar":
cmd = fmt.Sprintf("unrar -y e '%s' '%s'", f, b)
c = exec.Command("/bin/sh", "-c", cmd)
case ".zip":
cmd = fmt.Sprintf("unzip '%s' -d '%s'", f, b)
c = exec.Command("/bin/sh", "-c", cmd)
}
c.Dir = d
log.Println(d, cmd)
c.Stderr = os.Stderr
c.Stdout = os.Stdout
c.Run()
case strings.HasPrefix(op.Action, "label"):
to := strings.Split(op.Action, "=")
if len(to) > 1 {
m.Label = to[1]
} else {
m.Label = ""
}
meta.Set(k, m)
case strings.HasPrefix(op.Action, "icons"):
to := strings.Split(op.Action, "=")
m.Icons = []string{}
if len(to) > 1 {
m.Icons = append(m.Icons, to[1])
} else {
m.Icons = []string{}
}
meta.Set(k, m)
case op.Action == "star":
m.Star = !m.Star
meta.Set(k, m)
case strings.HasPrefix(op.Action, "star"):
to := strings.Split(op.Action, "=")
if len(to) > 1 && len(to[1]) > 0 {
m.Star = true
} else {
m.Star = false
}
meta.Set(k, m)
case op.Action == "delete":
log.Println(file, Trash)
trashMeta := kfs.NewMeta(Trash)
// delete trash, delete all file in trash
if file == Trash {
fs, _ := ioutil.ReadDir(Trash)
for _, v := range fs {
f := filepath.Join(Trash, v.Name())
log.Println("rm -rf", f)
os.RemoveAll(f)
}
} else if strings.HasPrefix(file, Trash) { // file inside trash, delete single file
log.Println("rm -rf", file)
os.RemoveAll(file)
meta.Del(k)
// trashMeta.Write()
} else { // not inside trash
dst := filepath.Join(Trash, k)
log.Println("mv", file, dst)
os.Rename(file, dst)
meta.Del(k)
m.OldLoc = file
trashMeta.Set(k, m)
trashMeta.Write()
}
cache.Remove("size:" + Trash)
}
}
}
meta.Write()
NewResp(w, "success")
// log.Println(toJSON(meta))
}