-
Notifications
You must be signed in to change notification settings - Fork 328
/
upload.go
94 lines (83 loc) · 2.12 KB
/
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2017 Google Inc.
//
// 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 stash
import (
"context"
"crypto/sha1"
"encoding/hex"
"hash"
"io"
"mime"
"path/filepath"
"sync"
"github.com/pkg/errors"
)
var sha1Pool = sync.Pool{New: func() interface{} { return sha1.New() }}
func hashStream(r io.Reader) string {
h := sha1Pool.Get().(hash.Hash)
defer sha1Pool.Put(h)
h.Reset()
io.Copy(h, r)
return hex.EncodeToString(h.Sum(nil))
}
func uploadStream(ctx context.Context, service Service, info Upload, r Uploadable) (string, error) {
// first calculate an id for the file
info.Id = hashStream(r)
// now check if the file is already in the stash
if entity, _ := service.Lookup(ctx, info.Id); entity != nil {
return info.Id, nil
}
// reset the stream and then upload the file to the specified id
if err := r.Reset(); err != nil {
return "", err
}
if len(info.Type) == 0 {
for _, name := range info.Name {
info.Type = append(info.Type, mime.TypeByExtension(filepath.Ext(name)))
}
}
w, err := service.Create(ctx, &info)
if err != nil {
return "", err
}
defer w.Close()
_, err = io.Copy(w, r)
if errors.Cause(err) == io.EOF {
err = nil
}
return info.Id, err
}
type seekadapter struct {
io.ReadSeeker
}
func (s seekadapter) Reset() error {
_, err := s.Seek(0, io.SeekStart)
return err
}
type bytesAdapter struct {
data []byte
offset int
}
func (b *bytesAdapter) Read(to []byte) (int, error) {
if b.offset >= len(b.data) {
return 0, io.EOF
}
count := copy(to, b.data[b.offset:])
b.offset += count
return count, nil
}
func (b *bytesAdapter) Reset() error {
b.offset = 0
return nil
}