mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-02-06 10:11:57 +08:00
Compare commits
2 Commits
3edb00a648
...
12e3f10ad7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12e3f10ad7 | ||
|
|
23d009d611 |
@@ -121,6 +121,9 @@ Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; verti
|
|||||||
{Name: "thumb_proxy_enabled", Value: "0", Type: "thumb"},
|
{Name: "thumb_proxy_enabled", Value: "0", Type: "thumb"},
|
||||||
{Name: "thumb_proxy_policy", Value: "[]", Type: "thumb"},
|
{Name: "thumb_proxy_policy", Value: "[]", Type: "thumb"},
|
||||||
{Name: "thumb_max_src_size", Value: "31457280", Type: "thumb"},
|
{Name: "thumb_max_src_size", Value: "31457280", Type: "thumb"},
|
||||||
|
{Name: "thumb_libraw_path", Value: "simple_dcraw", Type: "thumb"},
|
||||||
|
{Name: "thumb_libraw_enabled", Value: "0", Type: "thumb"},
|
||||||
|
{Name: "thumb_libraw_exts", Value: "arw,raf,dng", Type: "thumb"},
|
||||||
{Name: "pwa_small_icon", Value: "/static/img/favicon.ico", Type: "pwa"},
|
{Name: "pwa_small_icon", Value: "/static/img/favicon.ico", Type: "pwa"},
|
||||||
{Name: "pwa_medium_icon", Value: "/static/img/logo192.png", Type: "pwa"},
|
{Name: "pwa_medium_icon", Value: "/static/img/logo192.png", Type: "pwa"},
|
||||||
{Name: "pwa_large_icon", Value: "/static/img/logo512.png", Type: "pwa"},
|
{Name: "pwa_large_icon", Value: "/static/img/logo512.png", Type: "pwa"},
|
||||||
|
|||||||
@@ -145,6 +145,7 @@ func (fs *FileSystem) generateThumbnail(ctx context.Context, file *model.File) e
|
|||||||
"thumb_vips_enabled",
|
"thumb_vips_enabled",
|
||||||
"thumb_ffmpeg_enabled",
|
"thumb_ffmpeg_enabled",
|
||||||
"thumb_libreoffice_enabled",
|
"thumb_libreoffice_enabled",
|
||||||
|
"thumb_libraw_enabled",
|
||||||
))
|
))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = updateThumbStatus(file, model.ThumbStatusNotAvailable)
|
_ = updateThumbStatus(file, model.ThumbStatusNotAvailable)
|
||||||
|
|||||||
@@ -4,14 +4,15 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
|
||||||
"github.com/gofrs/uuid"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||||
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||||
|
"github.com/gofrs/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -24,10 +25,18 @@ type FfmpegGenerator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (*Result, error) {
|
func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (*Result, error) {
|
||||||
ffmpegOpts := model.GetSettingByNames("thumb_ffmpeg_path", "thumb_ffmpeg_exts", "thumb_ffmpeg_seek", "thumb_encode_method", "temp_path")
|
const (
|
||||||
|
thumbFFMpegPath = "thumb_ffmpeg_path"
|
||||||
|
thumbFFMpegExts = "thumb_ffmpeg_exts"
|
||||||
|
thumbFFMpegSeek = "thumb_ffmpeg_seek"
|
||||||
|
thumbEncodeMethod = "thumb_encode_method"
|
||||||
|
tempPath = "temp_path"
|
||||||
|
)
|
||||||
|
ffmpegOpts := model.GetSettingByNames(thumbFFMpegPath, thumbFFMpegExts, thumbFFMpegSeek, thumbEncodeMethod, tempPath)
|
||||||
|
|
||||||
if f.lastRawExts != ffmpegOpts["thumb_ffmpeg_exts"] {
|
if f.lastRawExts != ffmpegOpts[thumbFFMpegExts] {
|
||||||
f.exts = strings.Split(ffmpegOpts["thumb_ffmpeg_exts"], ",")
|
f.exts = strings.Split(ffmpegOpts[thumbFFMpegExts], ",")
|
||||||
|
f.lastRawExts = ffmpegOpts[thumbFFMpegExts]
|
||||||
}
|
}
|
||||||
|
|
||||||
if !util.IsInExtensionList(f.exts, name) {
|
if !util.IsInExtensionList(f.exts, name) {
|
||||||
@@ -35,16 +44,16 @@ func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, nam
|
|||||||
}
|
}
|
||||||
|
|
||||||
tempOutputPath := filepath.Join(
|
tempOutputPath := filepath.Join(
|
||||||
util.RelativePath(ffmpegOpts["temp_path"]),
|
util.RelativePath(ffmpegOpts[tempPath]),
|
||||||
"thumb",
|
"thumb",
|
||||||
fmt.Sprintf("thumb_%s.%s", uuid.Must(uuid.NewV4()).String(), ffmpegOpts["thumb_encode_method"]),
|
fmt.Sprintf("thumb_%s.%s", uuid.Must(uuid.NewV4()).String(), ffmpegOpts[thumbEncodeMethod]),
|
||||||
)
|
)
|
||||||
|
|
||||||
tempInputPath := src
|
tempInputPath := src
|
||||||
if tempInputPath == "" {
|
if tempInputPath == "" {
|
||||||
// If not local policy files, download to temp folder
|
// If not local policy files, download to temp folder
|
||||||
tempInputPath = filepath.Join(
|
tempInputPath = filepath.Join(
|
||||||
util.RelativePath(ffmpegOpts["temp_path"]),
|
util.RelativePath(ffmpegOpts[tempPath]),
|
||||||
"thumb",
|
"thumb",
|
||||||
fmt.Sprintf("ffmpeg_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)),
|
fmt.Sprintf("ffmpeg_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)),
|
||||||
)
|
)
|
||||||
@@ -68,7 +77,7 @@ func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, nam
|
|||||||
// Invoke ffmpeg
|
// Invoke ffmpeg
|
||||||
scaleOpt := fmt.Sprintf("scale=%s:%s:force_original_aspect_ratio=decrease", options["thumb_width"], options["thumb_height"])
|
scaleOpt := fmt.Sprintf("scale=%s:%s:force_original_aspect_ratio=decrease", options["thumb_width"], options["thumb_height"])
|
||||||
cmd := exec.CommandContext(ctx,
|
cmd := exec.CommandContext(ctx,
|
||||||
ffmpegOpts["thumb_ffmpeg_path"], "-ss", ffmpegOpts["thumb_ffmpeg_seek"], "-i", tempInputPath,
|
ffmpegOpts[thumbFFMpegPath], "-ss", ffmpegOpts[thumbFFMpegSeek], "-i", tempInputPath,
|
||||||
"-vf", scaleOpt, "-vframes", "1", tempOutputPath)
|
"-vf", scaleOpt, "-vframes", "1", tempOutputPath)
|
||||||
|
|
||||||
// Redirect IO
|
// Redirect IO
|
||||||
|
|||||||
283
pkg/thumb/libraw.go
Normal file
283
pkg/thumb/libraw.go
Normal file
@@ -0,0 +1,283 @@
|
|||||||
|
package thumb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/jpeg"
|
||||||
|
"image/png"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||||
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||||
|
"github.com/gofrs/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterGenerator(&LibRawGenerator{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type LibRawGenerator struct {
|
||||||
|
exts []string
|
||||||
|
lastRawExts string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *LibRawGenerator) Generate(ctx context.Context, file io.Reader, _ string, name string, options map[string]string) (*Result, error) {
|
||||||
|
const (
|
||||||
|
thumbLibRawPath = "thumb_libraw_path"
|
||||||
|
thumbLibRawExt = "thumb_libraw_exts"
|
||||||
|
thumbTempPath = "temp_path"
|
||||||
|
)
|
||||||
|
|
||||||
|
opts := model.GetSettingByNames(thumbLibRawPath, thumbLibRawExt, thumbTempPath)
|
||||||
|
|
||||||
|
if f.lastRawExts != opts[thumbLibRawExt] {
|
||||||
|
f.exts = strings.Split(opts[thumbLibRawExt], ",")
|
||||||
|
f.lastRawExts = opts[thumbLibRawExt]
|
||||||
|
}
|
||||||
|
|
||||||
|
if !util.IsInExtensionList(f.exts, name) {
|
||||||
|
return nil, fmt.Errorf("unsupported image format: %w", ErrPassThrough)
|
||||||
|
}
|
||||||
|
|
||||||
|
inputFilePath := filepath.Join(
|
||||||
|
util.RelativePath(opts[thumbTempPath]),
|
||||||
|
"thumb",
|
||||||
|
fmt.Sprintf("thumb_%s", uuid.Must(uuid.NewV4()).String()),
|
||||||
|
)
|
||||||
|
defer func() { _ = os.Remove(inputFilePath) }()
|
||||||
|
|
||||||
|
inputFile, err := util.CreatNestedFile(inputFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create temp file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = io.Copy(inputFile, file); err != nil {
|
||||||
|
_ = inputFile.Close()
|
||||||
|
return nil, fmt.Errorf("failed to write input file: %w", err)
|
||||||
|
}
|
||||||
|
_ = inputFile.Close()
|
||||||
|
|
||||||
|
cmd := exec.CommandContext(ctx, opts[thumbLibRawPath], "-e", inputFilePath)
|
||||||
|
|
||||||
|
var stdErr bytes.Buffer
|
||||||
|
cmd.Stderr = &stdErr
|
||||||
|
if err = cmd.Run(); err != nil {
|
||||||
|
util.Log().Warning("Failed to invoke LibRaw: %s", stdErr.String())
|
||||||
|
return nil, fmt.Errorf("failed to invoke LibRaw: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
outputFilePath := inputFilePath + ".thumb.jpg"
|
||||||
|
defer func() { _ = os.Remove(outputFilePath) }()
|
||||||
|
|
||||||
|
ff, err := os.Open(outputFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to open temp file: %w", err)
|
||||||
|
}
|
||||||
|
defer func() { _ = ff.Close() }()
|
||||||
|
|
||||||
|
// use builtin generator
|
||||||
|
result, err := new(Builtin).Generate(ctx, ff, outputFilePath, filepath.Base(outputFilePath), options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to generate thumbnail: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
orientation, err := getJpegOrientation(outputFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get jpeg orientation: %w", err)
|
||||||
|
}
|
||||||
|
if orientation == 1 {
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = rotateImg(result.Path, orientation); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to rotate image: %w", err)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func rotateImg(filePath string, orientation int) error {
|
||||||
|
resultImg, err := os.OpenFile(filePath, os.O_RDWR, 0777)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() { _ = resultImg.Close() }()
|
||||||
|
|
||||||
|
imgFlag := make([]byte, 3)
|
||||||
|
if _, err = io.ReadFull(resultImg, imgFlag); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err = resultImg.Seek(0, 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var img image.Image
|
||||||
|
if bytes.Equal(imgFlag, []byte{0xFF, 0xD8, 0xFF}) {
|
||||||
|
img, err = jpeg.Decode(resultImg)
|
||||||
|
} else {
|
||||||
|
img, err = png.Decode(resultImg)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch orientation {
|
||||||
|
case 8:
|
||||||
|
img = rotate90(img)
|
||||||
|
case 3:
|
||||||
|
img = rotate90(rotate90(img))
|
||||||
|
case 6:
|
||||||
|
img = rotate90(rotate90(rotate90(img)))
|
||||||
|
case 2:
|
||||||
|
img = mirrorImg(img)
|
||||||
|
case 7:
|
||||||
|
img = rotate90(mirrorImg(img))
|
||||||
|
case 4:
|
||||||
|
img = rotate90(rotate90(mirrorImg(img)))
|
||||||
|
case 5:
|
||||||
|
img = rotate90(rotate90(rotate90(mirrorImg(img))))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = resultImg.Truncate(0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err = resultImg.Seek(0, 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if bytes.Equal(imgFlag, []byte{0xFF, 0xD8, 0xFF}) {
|
||||||
|
return jpeg.Encode(resultImg, img, nil)
|
||||||
|
}
|
||||||
|
return png.Encode(resultImg, img)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getJpegOrientation(fileName string) (int, error) {
|
||||||
|
f, err := os.Open(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
|
header := make([]byte, 6)
|
||||||
|
defer func() { header = nil }()
|
||||||
|
if _, err = io.ReadFull(f, header); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// jpeg format header
|
||||||
|
if !bytes.Equal(header[:3], []byte{0xFF, 0xD8, 0xFF}) {
|
||||||
|
return 0, errors.New("not a jpeg")
|
||||||
|
}
|
||||||
|
|
||||||
|
// not a APP1 marker
|
||||||
|
if header[3] != 0xE1 {
|
||||||
|
return 1, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// exif data total length
|
||||||
|
totalLen := int(header[4])<<8 + int(header[5]) - 2
|
||||||
|
buf := make([]byte, totalLen)
|
||||||
|
defer func() { buf = nil }()
|
||||||
|
if _, err = io.ReadFull(f, buf); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove Exif identifier code
|
||||||
|
buf = buf[6:]
|
||||||
|
|
||||||
|
// byte order
|
||||||
|
parse16, parse32, err := initParseMethod(buf[:2])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// version
|
||||||
|
_ = buf[2:4]
|
||||||
|
|
||||||
|
// first IFD offset
|
||||||
|
offset := parse32(buf[4:8])
|
||||||
|
|
||||||
|
// first DE offset
|
||||||
|
offset += 2
|
||||||
|
buf = buf[offset:]
|
||||||
|
|
||||||
|
const (
|
||||||
|
orientationTag = 0x112
|
||||||
|
deEntryLength = 12
|
||||||
|
)
|
||||||
|
for len(buf) > deEntryLength {
|
||||||
|
tag := parse16(buf[:2])
|
||||||
|
if tag == orientationTag {
|
||||||
|
return int(parse32(buf[8:12])), nil
|
||||||
|
}
|
||||||
|
buf = buf[deEntryLength:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, errors.New("orientation not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func initParseMethod(buf []byte) (func([]byte) int16, func([]byte) int32, error) {
|
||||||
|
if bytes.Equal(buf, []byte{0x49, 0x49}) {
|
||||||
|
return littleEndian16, littleEndian32, nil
|
||||||
|
}
|
||||||
|
if bytes.Equal(buf, []byte{0x4D, 0x4D}) {
|
||||||
|
return bigEndian16, bigEndian32, nil
|
||||||
|
}
|
||||||
|
return nil, nil, errors.New("invalid byte order")
|
||||||
|
}
|
||||||
|
|
||||||
|
func littleEndian16(buf []byte) int16 {
|
||||||
|
return int16(buf[0]) | int16(buf[1])<<8
|
||||||
|
}
|
||||||
|
|
||||||
|
func bigEndian16(buf []byte) int16 {
|
||||||
|
return int16(buf[1]) | int16(buf[0])<<8
|
||||||
|
}
|
||||||
|
|
||||||
|
func littleEndian32(buf []byte) int32 {
|
||||||
|
return int32(buf[0]) | int32(buf[1])<<8 | int32(buf[2])<<16 | int32(buf[3])<<24
|
||||||
|
}
|
||||||
|
|
||||||
|
func bigEndian32(buf []byte) int32 {
|
||||||
|
return int32(buf[3]) | int32(buf[2])<<8 | int32(buf[1])<<16 | int32(buf[0])<<24
|
||||||
|
}
|
||||||
|
|
||||||
|
func rotate90(img image.Image) image.Image {
|
||||||
|
bounds := img.Bounds()
|
||||||
|
width, height := bounds.Dx(), bounds.Dy()
|
||||||
|
newImg := image.NewRGBA(image.Rect(0, 0, height, width))
|
||||||
|
for x := 0; x < width; x++ {
|
||||||
|
for y := 0; y < height; y++ {
|
||||||
|
newImg.Set(y, width-x-1, img.At(x, y))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newImg
|
||||||
|
}
|
||||||
|
|
||||||
|
func mirrorImg(img image.Image) image.Image {
|
||||||
|
bounds := img.Bounds()
|
||||||
|
width, height := bounds.Dx(), bounds.Dy()
|
||||||
|
newImg := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||||
|
for x := 0; x < width; x++ {
|
||||||
|
for y := 0; y < height; y++ {
|
||||||
|
newImg.Set(width-x-1, y, img.At(x, y))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newImg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *LibRawGenerator) Priority() int {
|
||||||
|
return 250
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *LibRawGenerator) EnableFlag() string {
|
||||||
|
return "thumb_libraw_enabled"
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Generator = (*LibRawGenerator)(nil)
|
||||||
@@ -4,14 +4,15 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
|
||||||
"github.com/gofrs/uuid"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||||
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||||
|
"github.com/gofrs/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -24,10 +25,17 @@ type LibreOfficeGenerator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src string, name string, options map[string]string) (*Result, error) {
|
func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src string, name string, options map[string]string) (*Result, error) {
|
||||||
sofficeOpts := model.GetSettingByNames("thumb_libreoffice_path", "thumb_libreoffice_exts", "thumb_encode_method", "temp_path")
|
const (
|
||||||
|
thumbLibreOfficePath = "thumb_libreoffice_path"
|
||||||
|
thumbLibreOfficeExts = "thumb_libreoffice_exts"
|
||||||
|
thumbEncodeMethod = "thumb_encode_method"
|
||||||
|
tempPath = "temp_path"
|
||||||
|
)
|
||||||
|
sofficeOpts := model.GetSettingByNames(thumbLibreOfficePath, thumbLibreOfficeExts, thumbEncodeMethod, tempPath)
|
||||||
|
|
||||||
if l.lastRawExts != sofficeOpts["thumb_libreoffice_exts"] {
|
if l.lastRawExts != sofficeOpts[thumbLibreOfficeExts] {
|
||||||
l.exts = strings.Split(sofficeOpts["thumb_libreoffice_exts"], ",")
|
l.exts = strings.Split(sofficeOpts[thumbLibreOfficeExts], ",")
|
||||||
|
l.lastRawExts = sofficeOpts[thumbLibreOfficeExts]
|
||||||
}
|
}
|
||||||
|
|
||||||
if !util.IsInExtensionList(l.exts, name) {
|
if !util.IsInExtensionList(l.exts, name) {
|
||||||
@@ -35,7 +43,7 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src
|
|||||||
}
|
}
|
||||||
|
|
||||||
tempOutputPath := filepath.Join(
|
tempOutputPath := filepath.Join(
|
||||||
util.RelativePath(sofficeOpts["temp_path"]),
|
util.RelativePath(sofficeOpts[tempPath]),
|
||||||
"thumb",
|
"thumb",
|
||||||
fmt.Sprintf("soffice_%s", uuid.Must(uuid.NewV4()).String()),
|
fmt.Sprintf("soffice_%s", uuid.Must(uuid.NewV4()).String()),
|
||||||
)
|
)
|
||||||
@@ -44,7 +52,7 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src
|
|||||||
if tempInputPath == "" {
|
if tempInputPath == "" {
|
||||||
// If not local policy files, download to temp folder
|
// If not local policy files, download to temp folder
|
||||||
tempInputPath = filepath.Join(
|
tempInputPath = filepath.Join(
|
||||||
util.RelativePath(sofficeOpts["temp_path"]),
|
util.RelativePath(sofficeOpts[tempPath]),
|
||||||
"thumb",
|
"thumb",
|
||||||
fmt.Sprintf("soffice_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)),
|
fmt.Sprintf("soffice_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)),
|
||||||
)
|
)
|
||||||
@@ -66,9 +74,9 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert the document to an image
|
// Convert the document to an image
|
||||||
cmd := exec.CommandContext(ctx, sofficeOpts["thumb_libreoffice_path"], "--headless",
|
cmd := exec.CommandContext(ctx, sofficeOpts[thumbLibreOfficePath], "--headless",
|
||||||
"-nologo", "--nofirststartwizard", "--invisible", "--norestore", "--convert-to",
|
"-nologo", "--nofirststartwizard", "--invisible", "--norestore", "--convert-to",
|
||||||
sofficeOpts["thumb_encode_method"], "--outdir", tempOutputPath, tempInputPath)
|
sofficeOpts[thumbEncodeMethod], "--outdir", tempOutputPath, tempInputPath)
|
||||||
|
|
||||||
// Redirect IO
|
// Redirect IO
|
||||||
var stdErr bytes.Buffer
|
var stdErr bytes.Buffer
|
||||||
@@ -83,7 +91,7 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src
|
|||||||
return &Result{
|
return &Result{
|
||||||
Path: filepath.Join(
|
Path: filepath.Join(
|
||||||
tempOutputPath,
|
tempOutputPath,
|
||||||
strings.TrimSuffix(filepath.Base(tempInputPath), filepath.Ext(tempInputPath))+"."+sofficeOpts["thumb_encode_method"],
|
strings.TrimSuffix(filepath.Base(tempInputPath), filepath.Ext(tempInputPath))+"."+sofficeOpts[thumbEncodeMethod],
|
||||||
),
|
),
|
||||||
Continue: true,
|
Continue: true,
|
||||||
Cleanup: []func(){func() { _ = os.RemoveAll(tempOutputPath) }},
|
Cleanup: []func(){func() { _ = os.RemoveAll(tempOutputPath) }},
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ func TestGenerator(ctx context.Context, name, executable string) (string, error)
|
|||||||
return testFfmpegGenerator(ctx, executable)
|
return testFfmpegGenerator(ctx, executable)
|
||||||
case "libreOffice":
|
case "libreOffice":
|
||||||
return testLibreOfficeGenerator(ctx, executable)
|
return testLibreOfficeGenerator(ctx, executable)
|
||||||
|
case "libRaw":
|
||||||
|
return testLibRawGenerator(ctx, executable)
|
||||||
default:
|
default:
|
||||||
return "", ErrUnknownGenerator
|
return "", ErrUnknownGenerator
|
||||||
}
|
}
|
||||||
@@ -72,3 +74,18 @@ func testLibreOfficeGenerator(ctx context.Context, executable string) (string, e
|
|||||||
|
|
||||||
return output.String(), nil
|
return output.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testLibRawGenerator(ctx context.Context, executable string) (string, error) {
|
||||||
|
cmd := exec.CommandContext(ctx, executable)
|
||||||
|
var output bytes.Buffer
|
||||||
|
cmd.Stdout = &output
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return "", fmt.Errorf("failed to invoke libraw executable: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(output.String(), "LibRaw") {
|
||||||
|
return "", ErrUnknownOutput
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.String(), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,13 +4,14 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
|
||||||
"github.com/gofrs/uuid"
|
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||||
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||||
|
"github.com/gofrs/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -23,10 +24,18 @@ type VipsGenerator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (*Result, error) {
|
func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (*Result, error) {
|
||||||
vipsOpts := model.GetSettingByNames("thumb_vips_path", "thumb_vips_exts", "thumb_encode_quality", "thumb_encode_method", "temp_path")
|
const (
|
||||||
|
thumbVipsPath = "thumb_vips_path"
|
||||||
|
thumbVipsExts = "thumb_vips_exts"
|
||||||
|
thumbEncodeQuality = "thumb_encode_quality"
|
||||||
|
thumbEncodeMethod = "thumb_encode_method"
|
||||||
|
tempPath = "temp_path"
|
||||||
|
)
|
||||||
|
vipsOpts := model.GetSettingByNames(thumbVipsPath, thumbVipsExts, thumbEncodeQuality, thumbEncodeMethod, tempPath)
|
||||||
|
|
||||||
if v.lastRawExts != vipsOpts["thumb_vips_exts"] {
|
if v.lastRawExts != vipsOpts[thumbVipsExts] {
|
||||||
v.exts = strings.Split(vipsOpts["thumb_vips_exts"], ",")
|
v.exts = strings.Split(vipsOpts[thumbVipsExts], ",")
|
||||||
|
v.lastRawExts = vipsOpts[thumbVipsExts]
|
||||||
}
|
}
|
||||||
|
|
||||||
if !util.IsInExtensionList(v.exts, name) {
|
if !util.IsInExtensionList(v.exts, name) {
|
||||||
@@ -34,21 +43,21 @@ func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name
|
|||||||
}
|
}
|
||||||
|
|
||||||
outputOpt := ".png"
|
outputOpt := ".png"
|
||||||
if vipsOpts["thumb_encode_method"] == "jpg" {
|
if vipsOpts[thumbEncodeMethod] == "jpg" {
|
||||||
outputOpt = fmt.Sprintf(".jpg[Q=%s]", vipsOpts["thumb_encode_quality"])
|
outputOpt = fmt.Sprintf(".jpg[Q=%s]", vipsOpts[thumbEncodeQuality])
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.CommandContext(ctx,
|
cmd := exec.CommandContext(ctx,
|
||||||
vipsOpts["thumb_vips_path"], "thumbnail_source", "[descriptor=0]", outputOpt, options["thumb_width"],
|
vipsOpts[thumbVipsPath], "thumbnail_source", "[descriptor=0]", outputOpt, options["thumb_width"],
|
||||||
"--height", options["thumb_height"])
|
"--height", options["thumb_height"])
|
||||||
|
|
||||||
tempPath := filepath.Join(
|
outTempPath := filepath.Join(
|
||||||
util.RelativePath(vipsOpts["temp_path"]),
|
util.RelativePath(vipsOpts[tempPath]),
|
||||||
"thumb",
|
"thumb",
|
||||||
fmt.Sprintf("thumb_%s", uuid.Must(uuid.NewV4()).String()),
|
fmt.Sprintf("thumb_%s", uuid.Must(uuid.NewV4()).String()),
|
||||||
)
|
)
|
||||||
|
|
||||||
thumbFile, err := util.CreatNestedFile(tempPath)
|
thumbFile, err := util.CreatNestedFile(outTempPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create temp file: %w", err)
|
return nil, fmt.Errorf("failed to create temp file: %w", err)
|
||||||
}
|
}
|
||||||
@@ -66,7 +75,7 @@ func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name
|
|||||||
return nil, fmt.Errorf("failed to invoke vips: %w", err)
|
return nil, fmt.Errorf("failed to invoke vips: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Result{Path: tempPath}, nil
|
return &Result{Path: outTempPath}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *VipsGenerator) Priority() int {
|
func (v *VipsGenerator) Priority() int {
|
||||||
|
|||||||
Reference in New Issue
Block a user