forked from ctdk/goiardi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_store.go
81 lines (76 loc) · 2.83 KB
/
file_store.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
/* Local server file storage, when we want to keep files locally and not load
* them up to S3. Pretty much the same sort of thing chef-zero does here. */
/*
* Copyright (c) 2013-2016, Jeremy Bingham (<[email protected]>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"github.com/ickymettle/goiardi/config"
"github.com/ickymettle/goiardi/filestore"
"net/http"
)
func fileStoreHandler(w http.ResponseWriter, r *http.Request) {
/* We *don't* always set the the content-type to application/json here,
* for obvious reasons. Still do for the PUT/POST though. */
chksum := r.URL.Path[12:]
/* Eventually, both local storage (in-memory or on disk, depending) or
* uploading to s3 or a similar cloud storage provider needs to be
* supported. */
switch r.Method {
case "GET":
w.Header().Set("Content-Type", "application/x-binary")
fileStore, err := filestore.Get(chksum)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.Write(*fileStore.Data)
case "PUT", "POST": /* Seems like for file uploads we ought to
* support POST too. */
w.Header().Set("Content-Type", "application/json")
/* Need to distinguish file already existing and some
* sort of error with uploading the file. */
if fileStore, _ := filestore.Get(chksum); fileStore != nil {
fileErr := fmt.Errorf("File with checksum %s already exists.", chksum)
/* Send status OK. It seems chef-pedant at least
* tries to upload files twice for some reason.
*/
jsonErrorReport(w, r, fileErr.Error(), http.StatusOK)
return
}
r.Body = http.MaxBytesReader(w, r.Body, config.Config.ObjMaxSize)
fileStore, err := filestore.New(chksum, r.Body, r.ContentLength)
if err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
err = fileStore.Save()
if err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
fileResponse := make(map[string]string)
fileResponse[fileStore.Chksum] = fmt.Sprintf("File with checksum %s uploaded.", fileStore.Chksum)
enc := json.NewEncoder(w)
if err := enc.Encode(&fileResponse); err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
}
/* Add DELETE later? */
default:
jsonErrorReport(w, r, "Unrecognized method!", http.StatusMethodNotAllowed)
}
}