File: simple.go

package info (click to toggle)
golang-github-backblaze-blazer 0.6.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 612 kB
  • sloc: makefile: 5
file content (109 lines) | stat: -rw-r--r-- 2,777 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2018, the Blazer authors
//
// 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 pyre

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"strconv"
	"strings"

	"github.com/Backblaze/blazer/internal/b2types"
	"github.com/google/uuid"
)

const uploadFilePrefix = "/b2api/v1/b2_upload_file/"

type SimpleFileManager interface {
	Writer(bucket, name, id string) (io.WriteCloser, error)
}

type simpleFileServer struct {
	fm SimpleFileManager
}

type uploadRequest struct {
	name        string
	contentType string
	size        int64
	sha1        string
	bucket      string
	info        map[string]string
}

func parseUploadHeaders(r *http.Request) (*uploadRequest, error) {
	ur := &uploadRequest{info: make(map[string]string)}
	ur.name = r.Header.Get("X-Bz-File-Name")
	ur.contentType = r.Header.Get("Content-Type")
	ur.sha1 = r.Header.Get("X-Bz-Content-Sha1")
	size, err := strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64)
	if err != nil {
		return nil, err
	}
	ur.size = size
	for k := range r.Header {
		if !strings.HasPrefix("X-Bz-Info-", k) {
			continue
		}
		name := strings.TrimPrefix("X-Bz-Info-", k)
		ur.info[name] = r.Header.Get(k)
	}
	ur.bucket = strings.TrimPrefix(r.URL.Path, uploadFilePrefix)
	return ur, nil
}

func (fs *simpleFileServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	req, err := parseUploadHeaders(r)
	if err != nil {
		http.Error(rw, err.Error(), 500)
		fmt.Println("oh no")
		return
	}
	id := uuid.New().String()
	w, err := fs.fm.Writer(req.bucket, req.name, id)
	if err != nil {
		http.Error(rw, err.Error(), 500)
		fmt.Println("oh no")
		return
	}
	if _, err := io.Copy(w, io.LimitReader(r.Body, req.size)); err != nil {
		w.Close()
		http.Error(rw, err.Error(), 500)
		fmt.Println("oh no")
		return
	}
	if err := w.Close(); err != nil {
		http.Error(rw, err.Error(), 500)
		fmt.Println("oh no")
		return
	}
	resp := &b2types.UploadFileResponse{
		FileID:   id,
		Name:     req.name,
		SHA1:     req.sha1,
		BucketID: req.bucket,
	}
	if err := json.NewEncoder(rw).Encode(resp); err != nil {
		http.Error(rw, err.Error(), 500)
		fmt.Println("oh no")
		return
	}
}

func RegisterSimpleFileManagerOnMux(f SimpleFileManager, mux *http.ServeMux) {
	mux.Handle(uploadFilePrefix, &simpleFileServer{fm: f})
}