From 58f8082795f87cbc6c6805dd4579f95f59fdd61f Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Sun, 28 Jul 2024 08:31:49 +0000 Subject: [PATCH] [bugfix] moves file rename to earlier in media pipeline so ffmpeg calls ALWAYS have extension (#3146) --- internal/media/ffmpeg.go | 43 +++++++++++++++++++------------ internal/media/processingemoji.go | 13 ++++++++++ internal/media/processingmedia.go | 22 +++++++++++----- 3 files changed, 55 insertions(+), 23 deletions(-) diff --git a/internal/media/ffmpeg.go b/internal/media/ffmpeg.go index 41727b3d9..ad76e5198 100644 --- a/internal/media/ffmpeg.go +++ b/internal/media/ffmpeg.go @@ -37,27 +37,25 @@ import ( ) // ffmpegClearMetadata generates a copy (in-place) of input media with all metadata cleared. -func ffmpegClearMetadata(ctx context.Context, filepath string, ext string) error { +func ffmpegClearMetadata(ctx context.Context, filepath string) error { + var outpath string + // Get directory from filepath. dirpath := path.Dir(filepath) - // Update filepath to add extension. - filepathExt := filepath + "." + ext - - // First we need to rename filepath to have extension. - if err := os.Rename(filepath, filepathExt); err != nil { - return gtserror.Newf("error renaming to %s - >%s: %w", filepath, filepathExt, err) + // Generate cleaned output path MAINTAINING extension. + if i := strings.IndexByte(filepath, '.'); i != -1 { + outpath = filepath[:i] + "_cleaned" + filepath[i:] + } else { + return gtserror.New("input file missing extension") } - // Generate cleaned output path with ext. - outpath := filepath + "_cleaned." + ext - // Clear metadata with ffmpeg. if err := ffmpeg(ctx, dirpath, "-loglevel", "error", // Input file path. - "-i", filepathExt, + "-i", filepath, // Drop all metadata. "-map_metadata", "-1", @@ -85,13 +83,18 @@ func ffmpegClearMetadata(ctx context.Context, filepath string, ext string) error // ffmpegGenerateThumb generates a thumbnail webp from input media of any type, useful for any media. func ffmpegGenerateThumb(ctx context.Context, filepath string, width, height int) (string, error) { + var outpath string + + // Generate thumb output path REPLACING extension. + if i := strings.IndexByte(filepath, '.'); i != -1 { + outpath = filepath[:i] + "_thumb.webp" + } else { + return "", gtserror.New("input file missing extension") + } // Get directory from filepath. dirpath := path.Dir(filepath) - // Generate output frame file path. - outpath := filepath + "_thumb.webp" - // Thumbnail size scaling argument. scale := strconv.Itoa(width) + ":" + strconv.Itoa(height) @@ -141,12 +144,18 @@ func ffmpegGenerateThumb(ctx context.Context, filepath string, width, height int // ffmpegGenerateStatic generates a static png from input image of any type, useful for emoji. func ffmpegGenerateStatic(ctx context.Context, filepath string) (string, error) { + var outpath string + + // Generate thumb output path REPLACING extension. + if i := strings.IndexByte(filepath, '.'); i != -1 { + outpath = filepath[:i] + "_static.png" + } else { + return "", gtserror.New("input file missing extension") + } + // Get directory from filepath. dirpath := path.Dir(filepath) - // Generate output static file path. - outpath := filepath + "_static.png" - // Generate static with ffmpeg. if err := ffmpeg(ctx, dirpath, "-loglevel", "error", diff --git a/internal/media/processingemoji.go b/internal/media/processingemoji.go index b4e96a946..696b78ed3 100644 --- a/internal/media/processingemoji.go +++ b/internal/media/processingemoji.go @@ -19,6 +19,7 @@ package media import ( "context" + "os" errorsv2 "codeberg.org/gruf/go-errors/v2" "codeberg.org/gruf/go-runners" @@ -169,6 +170,18 @@ func (p *ProcessingEmoji) store(ctx context.Context) error { return gtserror.Newf("unsupported emoji filetype: %s (%s)", fileType, ext) } + // Add file extension to path. + newpath := temppath + "." + ext + + // Before ffmpeg processing, rename to set file ext. + if err := os.Rename(temppath, newpath); err != nil { + return gtserror.Newf("error renaming to %s - >%s: %w", temppath, newpath, err) + } + + // Update path var + // AFTER successful. + temppath = newpath + // Generate a static image from input emoji path. staticpath, err = ffmpegGenerateStatic(ctx, temppath) if err != nil { diff --git a/internal/media/processingmedia.go b/internal/media/processingmedia.go index a5c60900b..32c0531bc 100644 --- a/internal/media/processingmedia.go +++ b/internal/media/processingmedia.go @@ -19,6 +19,7 @@ package media import ( "context" + "os" errorsv2 "codeberg.org/gruf/go-errors/v2" "codeberg.org/gruf/go-runners" @@ -185,15 +186,24 @@ func (p *ProcessingMedia) store(ctx context.Context) error { // Set media type from ffprobe format data. p.media.Type, ext = result.GetFileType() - switch p.media.Type { + // Add file extension to path. + newpath := temppath + "." + ext + + // Before ffmpeg processing, rename to set file ext. + if err := os.Rename(temppath, newpath); err != nil { + return gtserror.Newf("error renaming to %s - >%s: %w", temppath, newpath, err) + } + + // Update path var + // AFTER successful. + temppath = newpath + + switch p.media.Type { case gtsmodel.FileTypeImage, gtsmodel.FileTypeVideo: - // Pass file through ffmpeg clearing - // any excess metadata (e.g. EXIF). - if err := ffmpegClearMetadata(ctx, - temppath, ext, - ); err != nil { + // Pass file through ffmpeg clearing metadata (e.g. EXIF). + if err := ffmpegClearMetadata(ctx, temppath); err != nil { return gtserror.Newf("error cleaning metadata: %w", err) }