-
Notifications
You must be signed in to change notification settings - Fork 180
/
file-mime.go
144 lines (128 loc) · 3.82 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
/*
* 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 views
import (
"context"
"io"
"math"
"github.com/h2non/filetype"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/registry"
context2 "github.com/pydio/cells/common/utils/context"
)
const mimeReadLimit = 8192
type MimeResult struct {
mime string
err error
}
func (m *MimeResult) GetMime() string {
return m.mime
}
func (m *MimeResult) GetError() error {
return m.err
}
type TeeMimeReader struct {
r io.Reader
cb func(result *MimeResult)
waiter chan *MimeResult
limit int
data []byte
loadError error
loaded chan bool
done bool
}
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 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
}
func (m *TeeMimeReader) SetLimit(size int) {
m.limit = size
}
func (m *TeeMimeReader) Wait() chan *MimeResult {
m.waiter = make(chan *MimeResult, 1)
return m.waiter
}
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
}
var mimeMetaClient tree.NodeReceiverClient
func WrapReaderForMime(ctx context.Context, clone *tree.Node, reader io.Reader) io.Reader {
if mimeMetaClient == nil {
mimeMetaClient = tree.NewNodeReceiverClient(registry.GetClient(common.ServiceMeta))
}
bgCtx := context2.NewBackgroundWithMetaCopy(ctx)
return NewTeeMimeReader(reader, func(result *MimeResult) {
if result.GetError() == nil && result.GetMime() != "" {
// Store in metadata service
clone.MetaStore = make(map[string]string, 1)
clone.SetMeta(common.MetaNamespaceMime, result.GetMime())
if _, e := mimeMetaClient.CreateNode(bgCtx, &tree.CreateNodeRequest{
Node: clone,
UpdateIfExists: true,
}); e == nil {
log.Logger(ctx).Info("Stored mime type for node", clone.ZapUuid(), clone.ZapPath(), zap.String("mime", result.GetMime()))
} else {
log.Logger(ctx).Error("Could not update mime for node", zap.Error(e), clone.ZapUuid(), clone.ZapPath(), zap.String("mime", result.GetMime()))
}
}
})
}