Skip to content

Commit

Permalink
fix: temporary files not deleted, correctly check if files exist (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba authored Jul 11, 2024
1 parent be0edfd commit 8f79317
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 105 deletions.
18 changes: 8 additions & 10 deletions api/router/file_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,10 @@ func (r *FileRouter) Create(c *fiber.Ctx) error {
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
log.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
log.GetLogger().Error(err)
}
}(tmpPath)
file, err = r.fileSvc.Store(file.ID, service.StoreOptions{Path: &tmpPath}, userID)
Expand Down Expand Up @@ -221,11 +220,10 @@ func (r *FileRouter) Patch(c *fiber.Ctx) error {
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
log.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
log.GetLogger().Error(err)
}
}(tmpPath)
file, err = r.fileSvc.Store(file.ID, service.StoreOptions{Path: &tmpPath}, userID)
Expand Down
10 changes: 5 additions & 5 deletions conversion/pipeline/audio_video_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package pipeline

import (
"errors"
"os"
"path/filepath"

Expand Down Expand Up @@ -48,11 +49,10 @@ func (p *audioVideoPipeline) Run(opts client.PipelineRunOptions) error {
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(inputPath)
if err := p.apiClient.PatchTask(opts.TaskID, client.TaskPatchOptions{
Expand Down
19 changes: 9 additions & 10 deletions conversion/pipeline/glb_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package pipeline

import (
"errors"
"os"
"path/filepath"

Expand Down Expand Up @@ -48,11 +49,10 @@ func (p *glbPipeline) Run(opts client.PipelineRunOptions) error {
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(inputPath)
if err := p.apiClient.PatchTask(opts.TaskID, client.TaskPatchOptions{
Expand Down Expand Up @@ -85,11 +85,10 @@ func (p *glbPipeline) Run(opts client.PipelineRunOptions) error {
func (p *glbPipeline) createThumbnail(inputPath string, opts client.PipelineRunOptions) error {
tmpPath := filepath.FromSlash(os.TempDir() + "/" + helper.NewID() + ".jpeg")
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(tmpPath)
if err := p.glbProc.Thumbnail(inputPath, p.config.Limits.ImagePreviewMaxWidth, p.config.Limits.ImagePreviewMaxHeight, "rgb(255,255,255)", tmpPath); err != nil {
Expand Down
28 changes: 13 additions & 15 deletions conversion/pipeline/image_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package pipeline

import (
"errors"
"os"
"path/filepath"

Expand Down Expand Up @@ -49,11 +50,10 @@ func (p *imagePipeline) Run(opts client.PipelineRunOptions) error {
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(inputPath)
if err := p.apiClient.PatchTask(opts.TaskID, client.TaskPatchOptions{
Expand Down Expand Up @@ -136,11 +136,10 @@ func (p *imagePipeline) createThumbnail(inputPath string, opts client.PipelineRu
}
if *isAvailable {
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(tmpPath)
} else {
Expand Down Expand Up @@ -179,11 +178,10 @@ func (p *imagePipeline) convertTIFFToJPEG(inputPath string, imageProps client.Im
return nil, err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(jpegPath)
stat, err := os.Stat(jpegPath)
Expand Down
27 changes: 12 additions & 15 deletions conversion/pipeline/insights_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ func (p *insightsPipeline) Run(opts client.PipelineRunOptions) error {
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(inputPath)
if err := p.apiClient.PatchTask(opts.TaskID, client.TaskPatchOptions{
Expand Down Expand Up @@ -108,11 +107,10 @@ func (p *insightsPipeline) createText(inputPath string, opts client.PipelineRunO
return nil, err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(noAlphaImagePath)
/* Convert to PDF/A */
Expand All @@ -121,11 +119,10 @@ func (p *insightsPipeline) createText(inputPath string, opts client.PipelineRunO
return nil, err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(pdfPath)
/* Set OCR S3 object */
Expand Down
9 changes: 4 additions & 5 deletions conversion/pipeline/mosaic_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ func (p *mosaicPipeline) Run(opts client.PipelineRunOptions) error {
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(inputPath)
if err := p.apiClient.PatchTask(opts.TaskID, client.TaskPatchOptions{
Expand Down
10 changes: 5 additions & 5 deletions conversion/pipeline/office_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package pipeline

import (
"errors"
"os"
"path/filepath"

Expand Down Expand Up @@ -84,11 +85,10 @@ func (p *officePipeline) convertToPDF(inputPath string, opts client.PipelineRunO
return nil, err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(*outputPath)
defer func(path string) {
Expand Down
19 changes: 9 additions & 10 deletions conversion/pipeline/pdf_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package pipeline

import (
"errors"
"os"
"path/filepath"

Expand Down Expand Up @@ -51,11 +52,10 @@ func (p *pdfPipeline) Run(opts client.PipelineRunOptions) error {
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(inputPath)
if err := p.apiClient.PatchTask(opts.TaskID, client.TaskPatchOptions{
Expand Down Expand Up @@ -101,11 +101,10 @@ func (p *pdfPipeline) createThumbnail(inputPath string, opts client.PipelineRunO
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(tmpPath)
props, err := p.imageProc.MeasureImage(tmpPath)
Expand Down
19 changes: 9 additions & 10 deletions conversion/pipeline/zip_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package pipeline

import (
"errors"
"os"
"path/filepath"

Expand Down Expand Up @@ -50,11 +51,10 @@ func (p *zipPipeline) Run(opts client.PipelineRunOptions) error {
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(inputPath)
isGLTF, err := p.fi.IsGLTF(inputPath)
Expand Down Expand Up @@ -113,11 +113,10 @@ func (p *zipPipeline) convertToGLB(inputPath string, opts client.PipelineRunOpti
return nil, err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(outputPath)
stat, err := os.Stat(outputPath)
Expand Down
3 changes: 1 addition & 2 deletions conversion/processor/pdf_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package processor
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -46,7 +45,7 @@ func (p *PDFProcessor) TextFromPDF(inputPath string) (*string, error) {
}

defer func(path string) {
if err := os.Remove(path); errors.Is(err, fs.ErrNotExist) {
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
Expand Down
10 changes: 5 additions & 5 deletions conversion/processor/video_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package processor

import (
"errors"
"os"
"path/filepath"

Expand Down Expand Up @@ -39,11 +40,10 @@ func (p *VideoProcessor) Thumbnail(inputPath string, width int, height int, outp
return err
}
defer func(path string) {
_, err := os.Stat(path)
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
infra.GetLogger().Error(err)
}
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(tmpPath)
if err := p.imageProc.ResizeImage(tmpPath, width, height, outputPath); err != nil {
Expand Down
Loading

0 comments on commit 8f79317

Please sign in to comment.