-
Notifications
You must be signed in to change notification settings - Fork 171
/
file-mime.go
153 lines (137 loc) · 4.16 KB
/
file-mime.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package nodes
import (
"context"
"io"
"math"
"net/http"
"github.com/h2non/filetype"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/proto/tree"
"github.com/pydio/cells/v4/common/runtime"
"github.com/pydio/cells/v4/common/service/context/metadata"
)
const (
mimeReadLimit = 8192
mimeDefault = "application/octet-stream"
)
type MimeResult struct {
mime string
err error
}
func (m *MimeResult) GetMime() string {
return m.mime
}
func (m *MimeResult) GetError() error {
return m.err
}
// TeeMimeReader dynamically looks up for mimetype while consuming the io.Reader
type TeeMimeReader struct {
r io.Reader
cb func(result *MimeResult)
waiter chan *MimeResult
limit int
data []byte
loadError error
loaded chan bool
done bool
}
// NewTeeMimeReader creates a TeeMimeReader from an existing reader and calls the callbackRoutine once
// the mimetype is guessed
func NewTeeMimeReader(reader io.Reader, callbackRoutine func(result *MimeResult)) *TeeMimeReader {
mr := &TeeMimeReader{
r: reader,
cb: callbackRoutine,
data: make([]byte, 0, 8192),
loaded: make(chan bool, 1),
limit: mimeReadLimit,
}
go func() {
<-mr.loaded
//fmt.Printf("Stored %d bytes in buffer - Error: %v\n", len(mr.data), mr.loadError)
kind, _ := filetype.Match(mr.data)
mime := kind.MIME.Value
if mime == mimeDefault || mime == "" {
mime = http.DetectContentType(mr.data)
}
if callbackRoutine != nil {
callbackRoutine(&MimeResult{mime: mime, err: mr.loadError})
}
if mr.waiter != nil {
mr.waiter <- &MimeResult{mime: mime, err: mr.loadError}
close(mr.waiter)
}
}()
return mr
}
// SetLimit can override default mimeReadLimit (used mainly for testing)
func (m *TeeMimeReader) SetLimit(size int) {
m.limit = size
}
// Wait returns a blocking chan until a *MimeResult is returned
func (m *TeeMimeReader) Wait() chan *MimeResult {
m.waiter = make(chan *MimeResult, 1)
return m.waiter
}
// Read implements io.Reader interface by calling underlying reader Read.
func (m *TeeMimeReader) Read(p []byte) (n int, err error) {
n, err = m.r.Read(p)
if n > 0 && !m.done {
limit := int(math.Min(float64(n), float64(mimeReadLimit-len(m.data))))
if len(m.data) < mimeReadLimit {
//fmt.Println("Appending data", limit)
m.data = append(m.data, p[:limit]...)
}
if len(m.data) == mimeReadLimit {
m.done = true
close(m.loaded)
}
}
if err != nil && !m.done {
if err != io.EOF {
m.loadError = err
}
//fmt.Println("Close now", n, err)
m.done = true
close(m.loaded)
}
return
}
// WrapReaderForMime wraps a reader in a TeeMimeReader with a preset callback that stores detected mime in Metadata.
func WrapReaderForMime(ctx context.Context, clone *tree.Node, reader io.Reader) io.Reader {
if IsUnitTestEnv {
return reader
}
bgCtx := metadata.NewBackgroundWithMetaCopy(ctx)
bgCtx = runtime.ForkContext(bgCtx, ctx)
return NewTeeMimeReader(reader, func(result *MimeResult) {
mime := mimeDefault
if result.GetError() == nil && result.GetMime() != "" {
mime = result.GetMime()
}
// Store in metadata service
MustCoreMetaSet(bgCtx, clone.Uuid, common.MetaNamespaceMime, mime, clone.HasMetaKey(common.MetaNamespaceDatasourceInternal))
})
}
// IsDefaultMime checks if cType is empty or "application/octet-stream".
func IsDefaultMime(cType string) bool {
return cType != "" && cType != mimeDefault
}