/
dfs.uploadThemeFile_handler.go
182 lines (161 loc) · 5.22 KB
/
dfs.uploadThemeFile_handler.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Created from 'scheme.tl' by 'mtprotoc'
*
* Copyright (c) 2021-present, Teamgram Studio (https://teamgram.io).
* All rights reserved.
*
* Author: teamgramio (teamgram.io@gmail.com)
*/
package core
import (
"bytes"
"context"
"crypto/md5"
"fmt"
"image"
"math/rand"
"time"
"github.com/teamgram/marmota/pkg/bytes2"
"github.com/teamgram/marmota/pkg/threading2"
"github.com/teamgram/proto/mtproto"
"github.com/teamgram/teamgram-server/app/service/dfs/dfs"
"github.com/teamgram/teamgram-server/app/service/dfs/internal/dao"
"github.com/teamgram/teamgram-server/app/service/dfs/internal/imaging"
"github.com/teamgram/teamgram-server/app/service/dfs/internal/model"
)
// DfsUploadThemeFile
// dfs.uploadThemeFile flags:# creator:long file:InputFile thumb:flags.0?InputFile mime_type:string file_name:string = Document;
func (c *DfsCore) DfsUploadThemeFile(in *dfs.TLDfsUploadThemeFile) (*mtproto.Document, error) {
var (
path string
err error
documentId = c.svcCtx.Dao.IDGenClient2.NextId(c.ctx)
file = in.GetFile()
ext = model.GetFileExtName(file.GetName())
extType = model.GetStorageFileTypeConstructor(ext)
accessHash = int64(extType)<<32 | int64(rand.Uint32())
)
var (
thumbFile = in.GetThumb()
thumbSizeList []*mtproto.PhotoSize
)
// upload file
if file == nil {
c.Logger.Errorf("dfs.uploadThemeFile - ErrInputRequestInvalid")
return nil, mtproto.ErrWallpaperFileInvalid
}
if err = model.CheckFileParts(file.Parts); err != nil {
c.Logger.Errorf("dfs.uploadThemeFile - %v", err)
return nil, err
}
fileInfo, err := c.svcCtx.Dao.GetFileInfo(c.ctx, in.GetCreator(), file.Id)
if err != nil {
c.Logger.Errorf("dfs.uploadThemeFile - error: %v", err)
return nil, err
}
c.svcCtx.Dao.SetCacheFileInfo(c.ctx, documentId, fileInfo)
threading2.GoSafeContext(c.ctx, func(ctx context.Context) {
_, err2 := c.svcCtx.Dao.PutDocumentFile(
ctx,
fmt.Sprintf("%d.dat", documentId),
c.svcCtx.Dao.NewSSDBReader(fileInfo))
if err2 != nil {
c.Logger.Errorf("dfs.uploadThemeFile - error: %v", err2)
}
})
// upload thumb file
if thumbFile != nil {
var (
thumbCacheData []byte
thumb image.Image
// photoId = idgen.GetUUID()
// ext2 = request.GetThumb().GetName()
// extType2 = model.GetStorageFileTypeConstructor(ext2)
// secretId = int64(extType2)<<32 | int64(rand.Uint32())
r *dao.SSDBReader
)
r, err = c.svcCtx.Dao.OpenFile(c.ctx, in.GetCreator(), thumbFile.Id, thumbFile.Parts)
if err != nil {
c.Logger.Errorf("dfs.uploadThemeFile - %v", err)
return nil, mtproto.ErrThemeFileInvalid
}
thumbCacheData, err = r.ReadAll(c.ctx)
if err != nil {
c.Logger.Errorf("dfs.uploadThemeFile - %v", err)
return nil, mtproto.ErrThemeFileInvalid
}
if len(thumbFile.Md5Checksum) > 0 {
digest := fmt.Sprintf("%x", md5.Sum(thumbCacheData))
if digest != thumbFile.Md5Checksum {
c.Logger.Errorf("dfs.uploadThemeFile - (%s, %s) error: %v", digest, thumbFile.Md5Checksum, err)
return nil, mtproto.ErrCheckSumInvalid
}
}
// build photoStrippedSize
thumb, err = imaging.Decode(bytes.NewReader(thumbCacheData))
if err != nil {
c.Logger.Errorf("dfs.uploadThemeFile - error: %v", err)
return nil, err
}
stripped := bytes2.NewBuffer(make([]byte, 0, 4096))
if thumb.Bounds().Dx() >= thumb.Bounds().Dy() {
err = imaging.EncodeStripped(stripped, imaging.Resize(thumb, 40, 0), 30)
} else {
err = imaging.EncodeStripped(stripped, imaging.Resize(thumb, 0, 40), 30)
}
if err != nil {
c.Logger.Errorf("dfs.uploadThemeFile - error: %v", err)
return nil, err
}
// upload thumb
var (
mThumbData = bytes2.NewBuffer(make([]byte, 0, len(thumbCacheData)))
mThumb image.Image
)
if thumb.Bounds().Dx() >= thumb.Bounds().Dy() {
mThumb = imaging.Resize(thumb, 320, 0)
} else {
mThumb = imaging.Resize(thumb, 0, 320)
}
err = imaging.EncodeJpeg(mThumbData, mThumb)
if err != nil {
c.Logger.Errorf("dfs.uploadThemeFile - error: %v", err)
return nil, err
}
// upload thumb
path = fmt.Sprintf("%s/%d.dat", mtproto.PhotoSZMediumType, documentId)
// upload
c.svcCtx.Dao.PutPhotoFile(c.ctx, path, mThumbData.Bytes())
thumbSizeList = []*mtproto.PhotoSize{
mtproto.MakeTLPhotoStrippedSize(&mtproto.PhotoSize{
Type: mtproto.PhotoSZStrippedType,
Bytes: stripped.Bytes(),
}).To_PhotoSize(),
mtproto.MakeTLPhotoSize(&mtproto.PhotoSize{
Type: mtproto.PhotoSZMediumType,
W: int32(mThumb.Bounds().Dx()),
H: int32(mThumb.Bounds().Dy()),
Size2: int32(len(mThumbData.Bytes())),
}).To_PhotoSize(),
}
}
// build document
document := mtproto.MakeTLDocument(&mtproto.Document{
Id: documentId,
AccessHash: accessHash,
FileReference: []byte{}, // TODO(@benqi): gen file_reference
Date: int32(time.Now().Unix()),
MimeType: in.GetMimeType(),
Size2_INT32: int32(fileInfo.GetFileSize()),
Size2_INT64: fileInfo.GetFileSize(),
Thumbs: thumbSizeList,
VideoThumbs: nil,
DcId: 1,
Attributes: []*mtproto.DocumentAttribute{
mtproto.MakeTLDocumentAttributeFilename(&mtproto.DocumentAttribute{
FileName: in.GetFileName(),
}).To_DocumentAttribute(),
},
}).To_Document()
return document, nil
}