diff options
author | Anirudh Oppiliappan <x@icyphox.sh> | 2021-08-14 18:06:59 +0530 |
---|---|---|
committer | Anirudh Oppiliappan <x@icyphox.sh> | 2021-08-14 18:06:59 +0530 |
commit | b00f8f23450ac8f362a2eaa234199b820fbd17a3 (patch) | |
tree | 23121d1ed929025c019d7022105f5ed791fc5f91 | |
parent | 935cd56cdb007397f802d5f870f1c08496d87d19 (diff) | |
download | fsrv-b00f8f23450ac8f362a2eaa234199b820fbd17a3.tar.gz |
Add filetype hooks
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | hooks.go | 57 | ||||
-rwxr-xr-x | hooks/7z.sh | 6 | ||||
-rw-r--r-- | main.go | 8 |
5 files changed, 73 insertions, 2 deletions
@@ -1,3 +1,5 @@ | |||
1 | module git.icyphox.sh/fsrv | 1 | module git.icyphox.sh/fsrv |
2 | 2 | ||
3 | go 1.16 | 3 | go 1.16 |
4 | |||
5 | require github.com/h2non/filetype v1.1.1 | ||
@@ -0,0 +1,2 @@ | |||
1 | github.com/h2non/filetype v1.1.1 h1:xvOwnXKAckvtLWsN398qS9QhlxlnVXBjXBydK2/UFB4= | ||
2 | github.com/h2non/filetype v1.1.1/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= | ||
diff --git a/hooks.go b/hooks.go new file mode 100644 index 0000000..126a3b7 --- /dev/null +++ b/hooks.go | |||
@@ -0,0 +1,57 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "bufio" | ||
5 | "fmt" | ||
6 | "log" | ||
7 | "os" | ||
8 | "os/exec" | ||
9 | "path/filepath" | ||
10 | |||
11 | "github.com/h2non/filetype" | ||
12 | ) | ||
13 | |||
14 | func runHooks(file string) { | ||
15 | hooks, err := os.ReadDir("hooks") | ||
16 | if err != nil { | ||
17 | log.Println(err) | ||
18 | } | ||
19 | for _, h := range hooks { | ||
20 | hookFile := getHook(file) | ||
21 | if h.Name() == hookFile { | ||
22 | log.Println("running hook:", hookFile) | ||
23 | cmd := exec.Command(filepath.Join("hooks", h.Name()), file) | ||
24 | stdout, _ := cmd.StdoutPipe() | ||
25 | cmd.Start() | ||
26 | s := bufio.NewScanner(stdout) | ||
27 | for s.Scan() { | ||
28 | fmt.Println(s.Text()) | ||
29 | } | ||
30 | } | ||
31 | } | ||
32 | } | ||
33 | |||
34 | // Checks the MIME type of file and returns | ||
35 | // the corresponding hook file. | ||
36 | func getHook(file string) string { | ||
37 | // Not sure how many bytes the magic number takes, but 16 | ||
38 | // is a good guess. I think. | ||
39 | magic := make([]byte, 16) | ||
40 | |||
41 | f, err := os.Open(file) | ||
42 | if err != nil { | ||
43 | log.Println(err) | ||
44 | } | ||
45 | defer f.Close() | ||
46 | |||
47 | _, err = f.Read(magic) | ||
48 | if err != nil { | ||
49 | log.Println(err) | ||
50 | } | ||
51 | |||
52 | t, err := filetype.Match(magic) | ||
53 | if err != nil { | ||
54 | log.Println(err) | ||
55 | } | ||
56 | return t.Extension + ".sh" | ||
57 | } | ||
diff --git a/hooks/7z.sh b/hooks/7z.sh new file mode 100755 index 0000000..10a9636 --- /dev/null +++ b/hooks/7z.sh | |||
@@ -0,0 +1,6 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | mkdir tmp | ||
4 | 7z x "$1" -otmp | ||
5 | rsync -avz tmp/* jade:/mnt/music | ||
6 | rm -rf tmp | ||
@@ -36,13 +36,12 @@ func (s *settings) uploadFile(w http.ResponseWriter, r *http.Request) { | |||
36 | case "POST": | 36 | case "POST": |
37 | key := r.FormValue("key") | 37 | key := r.FormValue("key") |
38 | useragent := r.Header.Get("User-Agent") | 38 | useragent := r.Header.Get("User-Agent") |
39 | fmt.Println(key) | ||
40 | if key != s.key { | 39 | if key != s.key { |
41 | fmt.Fprintf(w, "incorrect key") | 40 | fmt.Fprintf(w, "incorrect key") |
42 | log.Printf("incorrect key: %+v", key) | 41 | log.Printf("incorrect key: %+v", key) |
43 | return | 42 | return |
44 | } | 43 | } |
45 | r.ParseMultipartForm(20 << 20) | 44 | r.ParseMultipartForm(512 << 20) |
46 | file, handler, err := r.FormFile("file") | 45 | file, handler, err := r.FormFile("file") |
47 | if err != nil { | 46 | if err != nil { |
48 | log.Println(err) | 47 | log.Println(err) |
@@ -61,6 +60,11 @@ func (s *settings) uploadFile(w http.ResponseWriter, r *http.Request) { | |||
61 | diskFile := filepath.Join(s.storepath, newFile) | 60 | diskFile := filepath.Join(s.storepath, newFile) |
62 | os.WriteFile(diskFile, fileBytes, 0644) | 61 | os.WriteFile(diskFile, fileBytes, 0644) |
63 | log.Printf("wrote: %+v", diskFile) | 62 | log.Printf("wrote: %+v", diskFile) |
63 | abs, err := filepath.Abs(diskFile) | ||
64 | if err != nil { | ||
65 | log.Println(err) | ||
66 | } | ||
67 | runHooks(abs) | ||
64 | 68 | ||
65 | fileUrl := s.url + "/" + newFile | 69 | fileUrl := s.url + "/" + newFile |
66 | if strings.Contains(useragent, "curl/") { | 70 | if strings.Contains(useragent, "curl/") { |