MM-27353 Mini image previews (#15376)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
2f47cf5994
Коммит
b8ceb610e6
52
app/file.go
52
app/file.go
@@ -633,7 +633,7 @@ func (a *App) UploadFileX(channelId, name string, input io.Reader,
|
||||
return t.fileinfo, aerr
|
||||
}
|
||||
|
||||
// Concurrently upload and update DB, and post-process the image.
|
||||
// Concurrently post-process the image.
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
if !t.Raw && t.fileinfo.IsImage() {
|
||||
@@ -649,6 +649,7 @@ func (a *App) UploadFileX(channelId, name string, input io.Reader,
|
||||
return nil, aerr
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
if _, err := t.saveToDatabase(t.fileinfo); err != nil {
|
||||
var appErr *model.AppError
|
||||
switch {
|
||||
@@ -659,8 +660,6 @@ func (a *App) UploadFileX(channelId, name string, input io.Reader,
|
||||
}
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return t.fileinfo, nil
|
||||
}
|
||||
|
||||
@@ -843,7 +842,7 @@ func (t *UploadFileTask) postprocessImage() {
|
||||
h := decoded.Bounds().Dy()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
wg.Add(3)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
thumb := decoded
|
||||
@@ -865,6 +864,13 @@ func (t *UploadFileTask) postprocessImage() {
|
||||
}
|
||||
writeJPEG(preview, t.fileinfo.PreviewPath)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if t.fileinfo.MiniPreview == nil {
|
||||
t.fileinfo.MiniPreview = model.GenerateMiniPreviewImage(decoded)
|
||||
}
|
||||
}()
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
@@ -1125,6 +1131,41 @@ func (a *App) generatePreviewImage(img image.Image, previewPath string, width in
|
||||
}
|
||||
}
|
||||
|
||||
// generateMiniPreview updates mini preview if needed
|
||||
// will save fileinfo with the preview added
|
||||
func (a *App) generateMiniPreview(fi *model.FileInfo) {
|
||||
if fi.IsImage() && fi.MiniPreview == nil {
|
||||
data, err := a.ReadFile(fi.Path)
|
||||
if err != nil {
|
||||
mlog.Error("error reading image file", mlog.Err(err))
|
||||
return
|
||||
}
|
||||
img, _, _ := prepareImage(data)
|
||||
if img == nil {
|
||||
return
|
||||
}
|
||||
fi.MiniPreview = model.GenerateMiniPreviewImage(img)
|
||||
if _, appErr := a.Srv().Store.FileInfo().Upsert(fi); appErr != nil {
|
||||
mlog.Error("creating mini preview failed", mlog.Err(appErr))
|
||||
} else {
|
||||
a.Srv().Store.FileInfo().InvalidateFileInfosForPostCache(fi.PostId, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) generateMiniPreviewForInfos(fileInfos []*model.FileInfo) {
|
||||
wg := new(sync.WaitGroup)
|
||||
|
||||
wg.Add(len(fileInfos))
|
||||
for _, fileInfo := range fileInfos {
|
||||
go func(fi *model.FileInfo) {
|
||||
defer wg.Done()
|
||||
a.generateMiniPreview(fi)
|
||||
}(fileInfo)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (a *App) GetFileInfo(fileId string) (*model.FileInfo, *model.AppError) {
|
||||
fileInfo, err := a.Srv().Store.FileInfo().Get(fileId)
|
||||
if err != nil {
|
||||
@@ -1137,6 +1178,7 @@ func (a *App) GetFileInfo(fileId string) (*model.FileInfo, *model.AppError) {
|
||||
}
|
||||
}
|
||||
|
||||
a.generateMiniPreview(fileInfo)
|
||||
return fileInfo, nil
|
||||
}
|
||||
|
||||
@@ -1155,6 +1197,8 @@ func (a *App) GetFileInfos(page, perPage int, opt *model.GetFileInfosOptions) ([
|
||||
}
|
||||
}
|
||||
|
||||
a.generateMiniPreviewForInfos(fileInfos)
|
||||
|
||||
return fileInfos, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1252,6 +1252,8 @@ func (a *App) GetFileInfosForPost(postId string, fromMaster bool) ([]*model.File
|
||||
return nil, model.NewAppError("GetFileInfosForPost", "app.file_info.get_for_post.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
a.generateMiniPreviewForInfos(fileInfos)
|
||||
|
||||
return fileInfos, nil
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user