feat(exiftool): refactor read write metadata

This commit is contained in:
Julien Neuhart
2024-03-23 11:13:05 +01:00
parent dc613aa2bf
commit 0a8625227f
22 changed files with 580 additions and 1138 deletions

View File

@@ -4,59 +4,25 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"github.com/barasher/go-exiftool"
"go.uber.org/zap"
"github.com/gotenberg/gotenberg/v8/pkg/gotenberg"
"github.com/gotenberg/gotenberg/v8/pkg/modules/api"
)
func init() {
gotenberg.MustRegisterModule(new(ExifTool))
}
// MetadataValueTypeError is constructed when metadata value types cannot be processed.
// The underlying library used in this implementation supports the writing of a limited
// number of metadata value types.
//
// For example, according to https://exiftool.org/TagNames/PDF.html metadata can be a boolean,
// i.e. "value format... may be string, date, integer, real, boolean or name".
// Furthermore, a native boolean type is also supported by JSON and Go. However, the
// underlying library does not currently support writing native Go boolean (bool) types.
// Therefore, an instance of this struct is created when a boolean metadata entry is supplied.
//
// The struct contains a key/value map corresponding to individual invalid metadata entries supplied by a consumer.
// This allows a helpful error message to be produced for API consumers.
// See API.WriteMetadata for more information on valid metadata value types.
type MetadataValueTypeError struct {
Entries map[string]interface{}
}
// Error returns a helpful error message.
func (e *MetadataValueTypeError) Error() string {
return fmt.Sprintf("invalid metadata value types supplied - identified by Entries: %s", e.Entries)
}
// GetKeys returns an array of keys with corresponding invalid value types,
func (e *MetadataValueTypeError) GetKeys() []string {
keys := make([]string, len(e.Entries))
i := 0
for key := range e.Entries {
keys[i] = key
i++
}
return keys
}
// ExifTool abstracts the CLI tool ExifTool and implements the [gotenberg.PdfEngine] interface .
// ExifTool abstracts the CLI tool ExifTool and implements the
// [gotenberg.PdfEngine] interface.
type ExifTool struct {
binPath string
}
// Descriptor returns ExifTool's module descriptor.
// Descriptor returns [ExifTool]'s module descriptor.
func (engine *ExifTool) Descriptor() gotenberg.ModuleDescriptor {
return gotenberg.ModuleDescriptor{
ID: "exiftool",
@@ -64,9 +30,7 @@ func (engine *ExifTool) Descriptor() gotenberg.ModuleDescriptor {
}
}
// Provision sets the module properties. It returns an error if
// - the environment variable EXIFTOOL_BIN_PATH is not set
// - there is an error creating an instance of exiftool.ExifTool
// Provision sets the module properties.
func (engine *ExifTool) Provision(ctx *gotenberg.Context) error {
binPath, ok := os.LookupEnv("EXIFTOOL_BIN_PATH")
if !ok {
@@ -90,99 +54,82 @@ func (engine *ExifTool) Validate() error {
// Merge is not available in this implementation.
func (engine *ExifTool) Merge(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
return fmt.Errorf("merge PDFs with LibreOffice: %w", gotenberg.ErrPdfEngineMethodNotSupported)
return fmt.Errorf("merge PDFs with ExifTool: %w", gotenberg.ErrPdfEngineMethodNotSupported)
}
// Convert is not available in this implementation.
func (engine *ExifTool) Convert(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error {
return fmt.Errorf("convert PDF to '%+v' with PDFtk: %w", formats, gotenberg.ErrPdfEngineMethodNotSupported)
return fmt.Errorf("convert PDF to '%+v' with ExifTool: %w", formats, gotenberg.ErrPdfEngineMethodNotSupported)
}
// ReadMetadata reads the metadata of the given PDF files.
func (engine *ExifTool) ReadMetadata(ctx context.Context, logger *zap.Logger, inputPath string, metadata map[string]interface{}) error {
logger.Debug(fmt.Sprintf("reading metadata of file: %s", inputPath))
exifTool, err := exiftool.NewExiftool()
// ReadMetadata extracts the metadata of a given PDF file.
func (engine *ExifTool) ReadMetadata(ctx context.Context, logger *zap.Logger, inputPath string) (map[string]interface{}, error) {
exifTool, err := exiftool.NewExiftool(exiftool.SetExiftoolBinaryPath(engine.binPath))
if err != nil {
fmt.Printf("Error intializing ExifTool: %v\n", err)
return err
return nil, fmt.Errorf("new ExifTool: %w", err)
}
fileMetadataInfos := exifTool.ExtractMetadata([]string{inputPath}...)
defer func(exifTool *exiftool.Exiftool) {
err := exifTool.Close()
if err != nil {
logger.Error(fmt.Sprintf("close ExifTool: %v", err))
}
}(exifTool)
if fileMetadataInfos[0].Err != nil {
return fmt.Errorf("error reading metadata to following file: %+v", fileMetadataInfos[0])
fileMetadata := exifTool.ExtractMetadata(inputPath)
if fileMetadata[0].Err != nil {
return nil, fmt.Errorf("read metadata with ExitfTool: %w", fileMetadata[0].Err)
}
// load into metadata
for k, v := range fileMetadataInfos[0].Fields {
metadata[k] = v
}
return exifTool.Close()
return fileMetadata[0].Fields, nil
}
// WriteMetadata write the metadata to the given PDF files.
func (engine *ExifTool) WriteMetadata(ctx context.Context, logger *zap.Logger, inputPath string, newMetadata map[string]interface{}) error {
logger.Debug(fmt.Sprintf("writing new metadata %s to %s", newMetadata, inputPath))
exifTool, err := exiftool.NewExiftool()
// WriteMetadata writes the metadata into a given PDF file.
func (engine *ExifTool) WriteMetadata(ctx context.Context, logger *zap.Logger, metadata map[string]interface{}, inputPath string) error {
exifTool, err := exiftool.NewExiftool(exiftool.SetExiftoolBinaryPath(engine.binPath))
if err != nil {
fmt.Printf("Error intializing ExifTool: %v\n", err)
return err
return fmt.Errorf("new ExifTool: %w", err)
}
fileMetadataInfos := exifTool.ExtractMetadata([]string{inputPath}...)
defer func(exifTool *exiftool.Exiftool) {
err := exifTool.Close()
if err != nil {
logger.Error(fmt.Sprintf("close ExifTool: %v", err))
}
}(exifTool)
// there is only file metadata info
if fileMetadataInfos[0].Err != nil {
return fmt.Errorf("error reading metadata to following file: %+v", fileMetadataInfos[0])
fileMetadata := exifTool.ExtractMetadata(inputPath)
if fileMetadata[0].Err != nil {
return fmt.Errorf("read metadata with ExitfTool: %w", fileMetadata[0].Err)
}
// Metadata values can only be specific value types.
// An error is returned if metadata with an invalid type is requested.
metadataValueErrors := MetadataValueTypeError{
Entries: make(map[string]interface{}),
}
// transform metadata
for key, value := range newMetadata {
for key, value := range metadata {
switch val := value.(type) {
case string:
fileMetadataInfos[0].SetString(key, val)
fileMetadata[0].SetString(key, val)
case int:
fileMetadataInfos[0].SetInt(key, int64(val))
fileMetadata[0].SetInt(key, int64(val))
case int64:
fileMetadataInfos[0].SetInt(key, val)
fileMetadata[0].SetInt(key, val)
case float32:
fileMetadataInfos[0].SetFloat(key, float64(val))
fileMetadata[0].SetFloat(key, float64(val))
case float64:
fileMetadataInfos[0].SetFloat(key, val)
fileMetadata[0].SetFloat(key, val)
case []string:
fileMetadataInfos[0].SetStrings(key, val)
// TODO: support more complex cases, e.g. arrays and nested objects (limitations in underlying library)
fileMetadata[0].SetStrings(key, val)
// TODO: support more complex cases, e.g., arrays and nested objects
// (limitations in underlying library).
default:
metadataValueErrors.Entries[key] = value
return fmt.Errorf("write PDF metadata with ExifTool: %w", gotenberg.ErrPdfEngineMetadataValueNotSupported)
}
}
logger.Debug(fmt.Sprintf("writing metadata %s to %s", fileMetadataInfos[0].Fields, fileMetadataInfos[0].File))
if len(metadataValueErrors.Entries) > 0 {
return api.WrapError(
fmt.Errorf("write metadata: %w", err),
api.NewSentinelHttpError(
http.StatusBadRequest,
fmt.Sprintf("Invalid metdata value types supplied by keys '%s'", metadataValueErrors.GetKeys())),
)
exifTool.WriteMetadata(fileMetadata)
if fileMetadata[0].Err != nil {
return fmt.Errorf("write PDF metadata with ExifTool: %w", fileMetadata[0].Err)
}
exifTool.WriteMetadata(fileMetadataInfos)
if fileMetadataInfos[0].Err != nil {
return fmt.Errorf("error writing metadata to following file: %+v", fileMetadataInfos[0])
}
return exifTool.Close()
return nil
}
// Interface guards.