-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp-upload.go
73 lines (62 loc) · 1.6 KB
/
http-upload.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
// Copyright 2021 Safecast. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
// Handle http requests to the root url
func httpUploadHandler(rsp http.ResponseWriter, req *http.Request) {
// Get the args
_, args, err := httpArgs(req, "")
if err != nil {
return
}
filename := args["file"]
if filename == "" {
io.WriteString(rsp, "file not specified")
return
}
collection := args["collection"]
if collection == "" {
io.WriteString(rsp, "collection not specified")
return
}
// Read the file
var contents []byte
contents, err = ioutil.ReadAll(req.Body)
if err != nil {
io.WriteString(rsp, fmt.Sprintf("can't read file: %s", err))
return
}
if len(contents) == 0 {
io.WriteString(rsp, "file not uploaded, or file is empty")
return
}
// Make sure that the collection directory is created
homedir := os.Getenv("HOME")
directory := homedir + filePathCollections + collection
file := directory + "/" + filename
os.MkdirAll(directory, 0777)
// Write the file
err = ioutil.WriteFile(file, contents, 0644)
if err != nil {
io.WriteString(rsp, fmt.Sprintf("can't write file: %s\n", err))
return
}
// Done
return
}
// Read from an upload
// Get a resource's contents
func uploadRead(collection string, filename string) (contents []byte, err error) {
homedir := os.Getenv("HOME")
directory := homedir + filePathCollections + collection
file := directory + "/" + filename
contents, err = ioutil.ReadFile(file)
return
}