Init V4 community edition (#2265)

* Init V4 community edition

* Init V4 community edition
This commit is contained in:
AaronLiu
2025-04-20 17:31:25 +08:00
committed by GitHub
parent da4e44b77a
commit 21d158db07
597 changed files with 119415 additions and 41692 deletions

View File

@@ -10,51 +10,46 @@ import (
"path/filepath"
"strings"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/manager/entitysource"
"github.com/cloudreve/Cloudreve/v4/pkg/logging"
"github.com/cloudreve/Cloudreve/v4/pkg/setting"
"github.com/cloudreve/Cloudreve/v4/pkg/util"
"github.com/gofrs/uuid"
)
func init() {
RegisterGenerator(&LibreOfficeGenerator{})
func NewLibreOfficeGenerator(l logging.Logger, settings setting.Provider) *LibreOfficeGenerator {
return &LibreOfficeGenerator{l: l, settings: settings}
}
type LibreOfficeGenerator struct {
exts []string
lastRawExts string
settings setting.Provider
l logging.Logger
}
func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src string, name string, options map[string]string) (*Result, error) {
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[thumbLibreOfficeExts] {
l.exts = strings.Split(sofficeOpts[thumbLibreOfficeExts], ",")
l.lastRawExts = sofficeOpts[thumbLibreOfficeExts]
func (l *LibreOfficeGenerator) Generate(ctx context.Context, es entitysource.EntitySource, ext string, previous *Result) (*Result, error) {
if !util.IsInExtensionListExt(l.settings.LibreOfficeThumbExts(ctx), ext) {
return nil, fmt.Errorf("unsupported video format: %w", ErrPassThrough)
}
if !util.IsInExtensionList(l.exts, name) {
return nil, fmt.Errorf("unsupported document format: %w", ErrPassThrough)
if es.Entity().Size() > l.settings.LibreOfficeThumbMaxSize(ctx) {
return nil, fmt.Errorf("file is too big: %w", ErrPassThrough)
}
tempOutputPath := filepath.Join(
util.RelativePath(sofficeOpts[tempPath]),
"thumb",
util.DataPath(l.settings.TempPath(ctx)),
thumbTempFolder,
fmt.Sprintf("soffice_%s", uuid.Must(uuid.NewV4()).String()),
)
tempInputPath := src
if tempInputPath == "" {
tempInputPath := ""
if es.IsLocal() {
tempInputPath = es.LocalPath(ctx)
} else {
// If not local policy files, download to temp folder
tempInputPath = filepath.Join(
util.RelativePath(sofficeOpts[tempPath]),
util.DataPath(l.settings.TempPath(ctx)),
"thumb",
fmt.Sprintf("soffice_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)),
fmt.Sprintf("soffice_%s.%s", uuid.Must(uuid.NewV4()).String(), ext),
)
// Due to limitations of ffmpeg, we need to write the input file to disk first
@@ -66,32 +61,32 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src
defer os.Remove(tempInputPath)
defer tempInputFile.Close()
if _, err = io.Copy(tempInputFile, file); err != nil {
return nil, fmt.Errorf("failed to write input file: %w", err)
if _, err = io.Copy(tempInputFile, es); err != nil {
return &Result{Path: tempOutputPath}, fmt.Errorf("failed to write input file: %w", err)
}
tempInputFile.Close()
}
// Convert the document to an image
cmd := exec.CommandContext(ctx, sofficeOpts[thumbLibreOfficePath], "--headless",
encode := l.settings.ThumbEncode(ctx)
cmd := exec.CommandContext(ctx, l.settings.LibreOfficePath(ctx), "--headless",
"-nologo", "--nofirststartwizard", "--invisible", "--norestore", "--convert-to",
sofficeOpts[thumbEncodeMethod], "--outdir", tempOutputPath, tempInputPath)
encode.Format, "--outdir", tempOutputPath, tempInputPath)
// Redirect IO
var stdErr bytes.Buffer
cmd.Stdin = file
cmd.Stderr = &stdErr
if err := cmd.Run(); err != nil {
util.Log().Warning("Failed to invoke LibreOffice: %s", stdErr.String())
return nil, fmt.Errorf("failed to invoke LibreOffice: %w", err)
l.l.Warning("Failed to invoke LibreOffice: %s", stdErr.String())
return &Result{Path: tempOutputPath}, fmt.Errorf("failed to invoke LibreOffice: %w, raw output: %s", err, stdErr.String())
}
return &Result{
Path: filepath.Join(
tempOutputPath,
strings.TrimSuffix(filepath.Base(tempInputPath), filepath.Ext(tempInputPath))+"."+sofficeOpts[thumbEncodeMethod],
strings.TrimSuffix(filepath.Base(tempInputPath), filepath.Ext(tempInputPath))+"."+encode.Format,
),
Continue: true,
Cleanup: []func(){func() { _ = os.RemoveAll(tempOutputPath) }},
@@ -102,6 +97,6 @@ func (l *LibreOfficeGenerator) Priority() int {
return 50
}
func (l *LibreOfficeGenerator) EnableFlag() string {
return "thumb_libreoffice_enabled"
func (l *LibreOfficeGenerator) Enabled(ctx context.Context) bool {
return l.settings.LibreOfficeThumbGeneratorEnabled(ctx)
}