ajout app

This commit is contained in:
2024-04-17 20:22:30 +02:00
parent cc017cfc5e
commit f9d05a2fd3
8025 changed files with 729805 additions and 0 deletions

View File

@ -0,0 +1,137 @@
package processor
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"voltaserve/client"
"voltaserve/config"
"voltaserve/core"
"voltaserve/helper"
"voltaserve/identifier"
"voltaserve/infra"
)
type ImageProcessor struct {
apiClient *client.APIClient
fileIdent *identifier.FileIdentifier
config config.Config
}
func NewImageProcessor() *ImageProcessor {
return &ImageProcessor{
apiClient: client.NewAPIClient(),
fileIdent: identifier.NewFileIdentifier(),
config: config.GetConfig(),
}
}
func (p *ImageProcessor) Base64Thumbnail(inputPath string) (core.ImageBase64, error) {
inputSize, err := p.MeasureImage(inputPath)
if err != nil {
return core.ImageBase64{}, err
}
if inputSize.Width > p.config.Limits.ImagePreviewMaxWidth || inputSize.Height > p.config.Limits.ImagePreviewMaxHeight {
outputPath := filepath.FromSlash(os.TempDir() + "/" + helper.NewID() + filepath.Ext(inputPath))
if inputSize.Width > inputSize.Height {
if err := p.ResizeImage(inputPath, p.config.Limits.ImagePreviewMaxWidth, 0, outputPath); err != nil {
return core.ImageBase64{}, err
}
} else {
if err := p.ResizeImage(inputPath, 0, p.config.Limits.ImagePreviewMaxHeight, outputPath); err != nil {
return core.ImageBase64{}, err
}
}
b64, err := helper.ImageToBase64(outputPath)
if err != nil {
return core.ImageBase64{}, err
}
size, err := p.MeasureImage(outputPath)
if err != nil {
return core.ImageBase64{}, err
}
return core.ImageBase64{
Base64: b64,
Width: size.Width,
Height: size.Height,
}, nil
} else {
b64, err := helper.ImageToBase64(inputPath)
if err != nil {
return core.ImageBase64{}, err
}
size, err := p.MeasureImage(inputPath)
if err != nil {
return core.ImageBase64{}, err
}
return core.ImageBase64{
Base64: b64,
Width: size.Width,
Height: size.Height,
}, nil
}
}
func (p *ImageProcessor) MeasureImage(inputPath string) (core.ImageProps, error) {
size, err := infra.NewCommand().ReadOutput("identify", "-format", "%w,%h", inputPath)
if err != nil {
return core.ImageProps{}, err
}
values := strings.Split(size, ",")
width, err := strconv.Atoi(helper.RemoveNonNumeric(values[0]))
if err != nil {
return core.ImageProps{}, err
}
height, err := strconv.Atoi(helper.RemoveNonNumeric(values[1]))
if err != nil {
return core.ImageProps{}, err
}
return core.ImageProps{Width: width, Height: height}, nil
}
func (p *ImageProcessor) ResizeImage(inputPath string, width int, height int, outputPath string) error {
var widthStr string
if width == 0 {
widthStr = ""
} else {
widthStr = strconv.FormatInt(int64(width), 10)
}
var heightStr string
if height == 0 {
heightStr = ""
} else {
heightStr = strconv.FormatInt(int64(height), 10)
}
if err := infra.NewCommand().Exec("convert", "-resize", widthStr+"x"+heightStr, inputPath, outputPath); err != nil {
return err
}
return nil
}
func (p *ImageProcessor) ThumbnailFromImage(inputPath string, width int, height int, outputPath string) error {
var widthStr string
if width == 0 {
widthStr = ""
} else {
widthStr = strconv.FormatInt(int64(width), 10)
}
var heightStr string
if height == 0 {
heightStr = ""
} else {
heightStr = strconv.FormatInt(int64(height), 10)
}
if err := infra.NewCommand().Exec("convert", "-thumbnail", widthStr+"x"+heightStr, "-background", "white", "-alpha", "remove", "-flatten", fmt.Sprintf("%s[0]", inputPath), outputPath); err != nil {
return err
}
return nil
}
func (p *ImageProcessor) ConvertImage(inputPath string, outputPath string) error {
if err := infra.NewCommand().Exec("convert", inputPath, outputPath); err != nil {
return err
}
return nil
}

View File

@ -0,0 +1,42 @@
package processor
import (
"go.uber.org/zap"
"os"
"path"
"path/filepath"
"strings"
"voltaserve/config"
"voltaserve/helper"
"voltaserve/infra"
)
type OfficeProcessor struct {
cmd *infra.Command
config config.Config
logger *zap.SugaredLogger
}
func NewOfficeProcessor() *OfficeProcessor {
logger, err := infra.GetLogger()
if err != nil {
panic(err)
}
return &OfficeProcessor{
cmd: infra.NewCommand(),
config: config.GetConfig(),
logger: logger,
}
}
func (p *OfficeProcessor) PDF(inputPath string) (string, error) {
outputDir := filepath.FromSlash(os.TempDir() + "/" + helper.NewID())
if err := infra.NewCommand().Exec("soffice", "--headless", "--convert-to", "pdf", "--outdir", outputDir, inputPath); err != nil {
return "", err
}
if _, err := os.Stat(inputPath); err != nil {
return "", err
}
base := filepath.Base(inputPath)
return filepath.FromSlash(outputDir + "/" + strings.TrimSuffix(base, path.Ext(base)) + ".pdf"), nil
}

View File

@ -0,0 +1,65 @@
package processor
import (
"os"
"path/filepath"
"strings"
"voltaserve/config"
"voltaserve/core"
"voltaserve/helper"
"voltaserve/infra"
)
type PDFProcessor struct {
cmd *infra.Command
imageProc *ImageProcessor
config config.Config
}
func NewPDFProcessor() *PDFProcessor {
return &PDFProcessor{
cmd: infra.NewCommand(),
imageProc: NewImageProcessor(),
config: config.GetConfig(),
}
}
func (p *PDFProcessor) Base64Thumbnail(inputPath string) (core.ImageBase64, error) {
outputPath := filepath.FromSlash(os.TempDir() + "/" + helper.NewID() + ".png")
if err := p.imageProc.ThumbnailFromImage(inputPath, 0, p.config.Limits.ImagePreviewMaxHeight, outputPath); err != nil {
return core.ImageBase64{}, err
}
b64, err := helper.ImageToBase64(outputPath)
if err != nil {
return core.ImageBase64{}, err
}
imageProps, err := p.imageProc.MeasureImage(outputPath)
if err != nil {
return core.ImageBase64{}, err
}
if _, err := os.Stat(outputPath); err == nil {
if err := os.Remove(outputPath); err != nil {
return core.ImageBase64{}, err
}
}
return core.ImageBase64{
Base64: b64,
Width: imageProps.Width,
Height: imageProps.Height,
}, nil
}
func (p *PDFProcessor) TextFromPDF(inputPath string) (string, error) {
outputPath := filepath.FromSlash(os.TempDir() + "/" + helper.NewID() + ".txt")
if err := infra.NewCommand().Exec("pdftotext", inputPath, outputPath); err != nil {
return "", err
}
b, err := os.ReadFile(outputPath)
if err != nil {
return "", err
}
if err := os.Remove(outputPath); err != nil {
return "", err
}
return strings.TrimSpace(string(b)), nil
}

View File

@ -0,0 +1,68 @@
package processor
import (
"go.uber.org/zap"
"os"
"path/filepath"
"voltaserve/config"
"voltaserve/core"
"voltaserve/helper"
"voltaserve/infra"
)
type VideoProcessor struct {
cmd *infra.Command
imageProc *ImageProcessor
config config.Config
logger *zap.SugaredLogger
}
func NewVideoProcessor() *VideoProcessor {
logger, err := infra.GetLogger()
if err != nil {
panic(err)
}
return &VideoProcessor{
cmd: infra.NewCommand(),
imageProc: NewImageProcessor(),
config: config.GetConfig(),
logger: logger,
}
}
func (p *VideoProcessor) Thumbnail(inputPath string, width int, height int, outputPath string) error {
tmpPath := filepath.FromSlash(os.TempDir() + "/" + helper.NewID() + ".png")
if err := infra.NewCommand().Exec("ffmpeg", "-i", inputPath, "-frames:v", "1", tmpPath); err != nil {
return err
}
if err := p.imageProc.ResizeImage(tmpPath, width, height, outputPath); err != nil {
return err
}
if err := os.Remove(tmpPath); err != nil {
return err
}
return nil
}
func (p *VideoProcessor) Base64Thumbnail(inputPath string) (core.ImageBase64, error) {
outputPath := filepath.FromSlash(os.TempDir() + "/" + helper.NewID() + ".png")
if err := p.Thumbnail(inputPath, 0, p.config.Limits.ImagePreviewMaxHeight, outputPath); err != nil {
return core.ImageBase64{}, err
}
b64, err := helper.ImageToBase64(outputPath)
if err != nil {
return core.ImageBase64{}, err
}
imageProps, err := p.imageProc.MeasureImage(outputPath)
if err != nil {
return core.ImageBase64{}, err
}
if err := os.Remove(outputPath); err != nil {
return core.ImageBase64{}, err
}
return core.ImageBase64{
Base64: b64,
Width: imageProps.Width,
Height: imageProps.Height,
}, nil
}