aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnirudh Oppiliappan <x@icyphox.sh>2021-08-14 18:06:59 +0530
committerAnirudh Oppiliappan <x@icyphox.sh>2021-08-14 18:06:59 +0530
commitb00f8f23450ac8f362a2eaa234199b820fbd17a3 (patch)
tree23121d1ed929025c019d7022105f5ed791fc5f91
parent935cd56cdb007397f802d5f870f1c08496d87d19 (diff)
downloadfsrv-b00f8f23450ac8f362a2eaa234199b820fbd17a3.tar.gz
Add filetype hooks
-rw-r--r--go.mod2
-rw-r--r--go.sum2
-rw-r--r--hooks.go57
-rwxr-xr-xhooks/7z.sh6
-rw-r--r--main.go8
5 files changed, 73 insertions, 2 deletions
diff --git a/go.mod b/go.mod
index 7840594..7c67c21 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
1module git.icyphox.sh/fsrv 1module git.icyphox.sh/fsrv
2 2
3go 1.16 3go 1.16
4
5require github.com/h2non/filetype v1.1.1
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..a13fb98
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
1github.com/h2non/filetype v1.1.1 h1:xvOwnXKAckvtLWsN398qS9QhlxlnVXBjXBydK2/UFB4=
2github.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 @@
1package main
2
3import (
4 "bufio"
5 "fmt"
6 "log"
7 "os"
8 "os/exec"
9 "path/filepath"
10
11 "github.com/h2non/filetype"
12)
13
14func 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.
36func 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
3mkdir tmp
47z x "$1" -otmp
5rsync -avz tmp/* jade:/mnt/music
6rm -rf tmp
diff --git a/main.go b/main.go
index f521dc1..f48b6bf 100644
--- a/main.go
+++ b/main.go
@@ -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/") {