|
|
@ -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) }},
|
|
|
|