diff options
author | Michael Muré <batolettre@gmail.com> | 2020-06-21 22:12:04 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-27 23:03:05 +0200 |
commit | 2ab6381a94d55fa22b80acdbb18849d6b24951f9 (patch) | |
tree | 99942b000955623ea7466b9fa4cc7dab37645df6 /api/http/git_file_upload_handler.go | |
parent | 5f72b04ef8e84b1c367ca6874519706318e351f5 (diff) | |
download | git-bug-2ab6381a94d55fa22b80acdbb18849d6b24951f9.tar.gz |
Reorganize the webUI and API code
Included in the changes:
- create a new /api root package to hold all API code, migrate /graphql in there
- git API handlers all use the cache instead of the repo directly
- git API handlers are now tested
- git API handlers now require a "repo" mux parameter
- lots of untangling of API/handlers/middleware
- less code in commands/webui.go
Diffstat (limited to 'api/http/git_file_upload_handler.go')
-rw-r--r-- | api/http/git_file_upload_handler.go | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/api/http/git_file_upload_handler.go b/api/http/git_file_upload_handler.go new file mode 100644 index 00000000..1702b8b1 --- /dev/null +++ b/api/http/git_file_upload_handler.go @@ -0,0 +1,104 @@ +package http + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + "github.com/gorilla/mux" + + "github.com/MichaelMure/git-bug/api/auth" + "github.com/MichaelMure/git-bug/cache" +) + +// implement a http.Handler that will accept and store content into git blob. +// +// Expected gorilla/mux parameters: +// - "repo" : the ref of the repo or "" for the default one +type gitUploadFileHandler struct { + mrc *cache.MultiRepoCache +} + +func NewGitUploadFileHandler(mrc *cache.MultiRepoCache) http.Handler { + return &gitUploadFileHandler{mrc: mrc} +} + +func (gufh *gitUploadFileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + var repo *cache.RepoCache + var err error + + repoVar := mux.Vars(r)["repo"] + switch repoVar { + case "": + repo, err = gufh.mrc.DefaultRepo() + default: + repo, err = gufh.mrc.ResolveRepo(repoVar) + } + + if err != nil { + http.Error(rw, "invalid repo reference", http.StatusBadRequest) + return + } + + _, err = auth.UserFromCtx(r.Context(), repo) + if err == auth.ErrNotAuthenticated { + http.Error(rw, "read-only mode or not logged in", http.StatusForbidden) + return + } else if err != nil { + http.Error(rw, fmt.Sprintf("loading identity: %v", err), http.StatusInternalServerError) + return + } + + // 100MB (github limit) + var maxUploadSize int64 = 100 * 1000 * 1000 + r.Body = http.MaxBytesReader(rw, r.Body, maxUploadSize) + if err := r.ParseMultipartForm(maxUploadSize); err != nil { + http.Error(rw, "file too big (100MB max)", http.StatusBadRequest) + return + } + + file, _, err := r.FormFile("uploadfile") + if err != nil { + http.Error(rw, "invalid file", http.StatusBadRequest) + return + } + defer file.Close() + fileBytes, err := ioutil.ReadAll(file) + if err != nil { + http.Error(rw, "invalid file", http.StatusBadRequest) + return + } + + filetype := http.DetectContentType(fileBytes) + if filetype != "image/jpeg" && filetype != "image/jpg" && + filetype != "image/gif" && filetype != "image/png" { + http.Error(rw, "invalid file type", http.StatusBadRequest) + return + } + + hash, err := repo.StoreData(fileBytes) + if err != nil { + http.Error(rw, err.Error(), http.StatusInternalServerError) + return + } + + type response struct { + Hash string `json:"hash"` + } + + resp := response{Hash: string(hash)} + + js, err := json.Marshal(resp) + if err != nil { + http.Error(rw, err.Error(), http.StatusInternalServerError) + return + } + + rw.Header().Set("Content-Type", "application/json") + _, err = rw.Write(js) + if err != nil { + http.Error(rw, err.Error(), http.StatusInternalServerError) + return + } +} |