-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathput.go
136 lines (117 loc) · 3.54 KB
/
put.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2018-2020 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package dataprovider
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/rhttp"
"github.com/cs3org/reva/pkg/token"
"github.com/eventials/go-tus"
"github.com/eventials/go-tus/memorystore"
)
func (s *svc) doPut(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := appctx.GetLogger(ctx)
fn := r.URL.Path
fsfn := strings.TrimPrefix(fn, s.conf.Prefix)
ref := &provider.Reference{Spec: &provider.Reference_Path{Path: fsfn}}
err := s.storage.Upload(ctx, ref, r.Body)
if err != nil {
log.Error().Err(err).Msg("error uploading file")
w.WriteHeader(http.StatusInternalServerError)
return
}
r.Body.Close()
w.WriteHeader(http.StatusOK)
}
func (s *svc) doTusPut(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := appctx.GetLogger(ctx)
fp := r.Header.Get("File-Path")
if fp == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
length, err := strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64)
if err != nil {
// Fallback to Upload-Length
length, err = strconv.ParseInt(r.Header.Get("Upload-Length"), 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
}
fd, err := ioutil.TempFile(fmt.Sprintf("/%s", s.conf.TempDirectory), "")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer os.RemoveAll(fd.Name())
defer fd.Close()
if _, err := io.Copy(fd, r.Body); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
dataServerURL := fmt.Sprintf("http://%s%s", r.Host, r.RequestURI)
// create the tus client.
c := tus.DefaultConfig()
c.Resume = true
c.HttpClient = rhttp.GetHTTPClient(
rhttp.Context(ctx),
rhttp.Timeout(time.Duration(s.conf.Timeout*int64(time.Second))),
rhttp.Insecure(s.conf.Insecure),
)
c.Store, err = memorystore.NewMemoryStore()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
c.Header.Set(token.TokenHeader, token.ContextMustGetToken(ctx))
tusc, err := tus.NewClient(dataServerURL, c)
if err != nil {
log.Error().Err(err).Msg("error starting TUS client")
w.WriteHeader(http.StatusInternalServerError)
return
}
metadata := map[string]string{
"filename": path.Base(fp),
"dir": path.Dir(fp),
}
upload := tus.NewUpload(fd, length, metadata, "")
defer r.Body.Close()
// create the uploader.
c.Store.Set(upload.Fingerprint, dataServerURL)
uploader := tus.NewUploader(tusc, dataServerURL, upload, 0)
// start the uploading process.
err = uploader.Upload()
if err != nil {
log.Error().Err(err).Msg("Could not start TUS upload")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}