summaryrefslogtreecommitdiff
path: root/pkg/api/file.go
blob: e2fa8d98592de477c8dff27348b62fa858d6290f (plain) (blame)
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
package api

import (
	"fmt"
	"net/http"
)

func (a *API) getFilelistHandler(w http.ResponseWriter, r *http.Request) {
	pauth := r.PathValue("pauth")
	if err := r.ParseForm(); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)

		return
	}
	form := r.PostForm
	if form.Has("pauth") {
		pauth = form.Get("pauth")
	}

	files, err := a.idec.FilesList(pauth)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)

		return
	}

	for _, file := range files {
		fmt.Fprintf(w, "%s:%d:%s\n", file.FullName, file.Size, file.Name)
	}
}

func (a *API) getFileHandler(w http.ResponseWriter, r *http.Request) {
	filename := r.PathValue("filename")
	pauth := ""
	if err := r.ParseForm(); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)

		return
	}
	form := r.PostForm
	if form.Has("pauth") {
		pauth = form.Get("pauth")
	}
	if form.Has("filename") {
		filename = form.Get("filename")
	}

	if err := a.idec.GetFile(pauth, filename, w); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
	}
}