Skip to content

Commit

Permalink
fix (plg_image_c): cleanup thumbnailer
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-kerjean committed Nov 13, 2023
1 parent 1777ac8 commit ade354f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
18 changes: 8 additions & 10 deletions server/plugin/plg_image_c/image_jpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@

#define JPEG_QUALITY 50

struct filestash_jpeg_error_mgr {
typedef struct filestash_jpeg_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf jmp;
};
} *filestash_jpeg_error_ptr;

typedef struct filestash_jpeg_error_mgr *filestash_jpeg_error_ptr;

void filestash_jpeg_error_exit (j_common_ptr cinfo) {
filestash_jpeg_error_ptr filestash_err = (filestash_jpeg_error_ptr) cinfo->err;
longjmp(filestash_err->jmp, 1);
}
void filestash_jpeg_error_exit (j_common_ptr cinfo);

int jpeg_to_jpeg(int inputDesc, int outputDesc, int targetSize) {
#ifdef HAS_DEBUG
Expand Down Expand Up @@ -123,8 +118,11 @@ int jpeg_to_jpeg(int inputDesc, int outputDesc, int targetSize) {
CLEANUP_AND_ABORT:
jpeg_destroy_decompress(&jpeg_config_input);
jpeg_destroy_compress(&jpeg_config_output);
fclose(input);
fclose(output);
DEBUG("final");
return status;
}

void filestash_jpeg_error_exit (j_common_ptr cinfo) {
filestash_jpeg_error_ptr filestash_err = (filestash_jpeg_error_ptr) cinfo->err;
longjmp(filestash_err->jmp, 1);
}
2 changes: 0 additions & 2 deletions server/plugin/plg_image_c/image_jpeg.h
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
#include <stdio.h>

int jpeg_to_jpeg(int input, int output, int targetSize);
3 changes: 1 addition & 2 deletions server/plugin/plg_image_c/image_png.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ int png_to_webp(int inputDesc, int outputDesc, int targetSize) {
goto CLEANUP_AND_ABORT_C;
}
fwrite(webp_output_data, webp_output_size, 1, output);
fflush(output);
DEBUG("after webp written");

CLEANUP_AND_ABORT_C:
Expand All @@ -125,7 +126,5 @@ int png_to_webp(int inputDesc, int outputDesc, int targetSize) {
if (png_ptr != NULL) png_destroy_read_struct(&png_ptr, (info_ptr != NULL) ? &info_ptr : NULL, NULL);

CLEANUP_AND_ABORT:
fclose(output);
fclose(input);
return status;
}
26 changes: 15 additions & 11 deletions server/plugin/plg_image_c/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
func init() {
Hooks.Register.Thumbnailer("image/jpeg", &transcoder{runner(jpeg), "image/jpeg", -200})
Hooks.Register.Thumbnailer("image/png", &transcoder{runner(png), "image/webp", -200})
Hooks.Register.Thumbnailer("image/gif", &transcoder{runner(gif), "image/webp", -300})
Hooks.Register.Thumbnailer("image/heic", &transcoder{runner(heif), "image/jpeg", -200})
rawMimeType := []string{
"image/x-canon-cr2", "image/x-tif", "image/x-canon-cr2", "image/x-canon-crw",
Expand Down Expand Up @@ -89,33 +90,36 @@ func (this transcoder) Generate(reader io.ReadCloser, ctx *App, res *http.Respon
func runner(fn func(uintptr, uintptr, int)) func(io.ReadCloser, int) (io.ReadCloser, error) {
return func(inputGo io.ReadCloser, size int) (io.ReadCloser, error) {
inputC, tmpw, err := os.Pipe()
logErrors(err, "plg_image_c::pipe")
if err != nil {
return nil, err
}
outputGo, outputC, err := os.Pipe()
logErrors(err, "plg_image_c::pipe")
if err != nil {
tmpw.Close()
Log.Stdout("ERR0 %+v", err.Error())
return nil, err
}

go func() {
fn(inputC.Fd(), outputC.Fd(), size) // <-- all this code so we can do that
inputC.Close()
outputC.Close()
logErrors(inputC.Close(), "plg_image_c::inputC")
logErrors(inputGo.Close(), "plg_image_c::inputGo")
logErrors(outputC.Close(), "plg_image_c::outputC")
}()
_, err = io.Copy(tmpw, inputGo)
inputGo.Close()
tmpw.Close()
if err != nil {
outputGo.Close()
Log.Stdout("ERR1 %+v", err.Error())
return nil, err
}
io.Copy(tmpw, inputGo)
logErrors(tmpw.Close(), "plg_image_c::tmpw")
return outputGo, nil
}
}

func logErrors(err error, msg string) {
if err == nil {
return
}
Log.Debug(msg + ": " + err.Error())
}

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
Expand Down

0 comments on commit ade354f

Please sign in to comment.