Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
773ab96b
Commit
773ab96b
authored
Sep 03, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update thumbnail generator
parent
09586d03
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
22 deletions
+15
-22
resource_service.go
server/router/api/v1/resource_service.go
+15
-22
No files found.
server/router/api/v1/resource_service.go
View file @
773ab96b
...
...
@@ -5,14 +5,12 @@ import (
"context"
"encoding/binary"
"fmt"
"image"
"io"
"log/slog"
"os"
"path/filepath"
"regexp"
"strings"
"sync/atomic"
"time"
"github.com/disintegration/imaging"
...
...
@@ -409,6 +407,11 @@ func (s *APIV1Service) GetResourceBlob(resource *store.Resource) ([]byte, error)
return
blob
,
nil
}
const
(
// thumbnailMaxWidth is the maximum width of the thumbnail image.
thumbnailMaxWidth
=
700
)
// getOrGenerateThumbnail returns the thumbnail image of the resource.
func
(
s
*
APIV1Service
)
getOrGenerateThumbnail
(
resource
*
store
.
Resource
)
([]
byte
,
error
)
{
thumbnailCacheFolder
:=
filepath
.
Join
(
s
.
Profile
.
Data
,
ThumbnailCacheFolder
)
...
...
@@ -421,16 +424,7 @@ func (s *APIV1Service) getOrGenerateThumbnail(resource *store.Resource) ([]byte,
return
nil
,
errors
.
Wrap
(
err
,
"failed to check thumbnail image stat"
)
}
var
availableGeneratorAmount
int32
=
32
if
atomic
.
LoadInt32
(
&
availableGeneratorAmount
)
<=
0
{
return
nil
,
errors
.
New
(
"not enough available generator amount"
)
}
atomic
.
AddInt32
(
&
availableGeneratorAmount
,
-
1
)
defer
func
()
{
atomic
.
AddInt32
(
&
availableGeneratorAmount
,
1
)
}()
// Otherwise, generate and save the thumbnail image.
// If thumbnail image does not exist, generate and save the thumbnail image.
blob
,
err
:=
s
.
GetResourceBlob
(
resource
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"failed to get resource blob"
)
...
...
@@ -440,29 +434,28 @@ func (s *APIV1Service) getOrGenerateThumbnail(resource *store.Resource) ([]byte,
return
nil
,
errors
.
Wrap
(
err
,
"failed to decode thumbnail image"
)
}
thumbnailMaxWidth
:=
700
// equal to home/explore screen image max width
var
thumbnailImage
image
.
Image
if
img
.
Bounds
()
.
Max
.
X
>
thumbnailMaxWidth
{
thumbnailImage
=
imaging
.
Resize
(
img
,
thumbnailMaxWidth
,
0
,
imaging
.
Lanczos
)
}
else
{
thumbnailImage
=
img
// If the image is smaller than the thumbnailMaxWidth, return the original image.
if
img
.
Bounds
()
.
Max
.
X
<
thumbnailMaxWidth
{
return
blob
,
nil
}
// Resize the image to the thumbnailMaxWidth.
thumbnailImage
:=
imaging
.
Resize
(
img
,
thumbnailMaxWidth
,
0
,
imaging
.
Lanczos
)
if
err
:=
imaging
.
Save
(
thumbnailImage
,
filePath
);
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"failed to save thumbnail file"
)
}
}
dst
File
,
err
:=
os
.
Open
(
filePath
)
thumbnail
File
,
err
:=
os
.
Open
(
filePath
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"failed to open thumbnail file"
)
}
defer
dst
File
.
Close
()
dstBlob
,
err
:=
io
.
ReadAll
(
dst
File
)
defer
thumbnail
File
.
Close
()
blob
,
err
:=
io
.
ReadAll
(
thumbnail
File
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"failed to read thumbnail file"
)
}
return
dstB
lob
,
nil
return
b
lob
,
nil
}
var
fileKeyPattern
=
regexp
.
MustCompile
(
`\{[a-z]{1,9}\}`
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment