diff --git a/cmd/fastgallery/main.go b/cmd/fastgallery/main.go index 60f338b..968e440 100644 --- a/cmd/fastgallery/main.go +++ b/cmd/fastgallery/main.go @@ -28,6 +28,10 @@ var assets embed.FS // Define global exit function, so unit tests can override this var exit = os.Exit +// Define global state for slice of WIP transformation jobs, used by signalHandler() +var wipJobs = make(map[string]transformationJob) +var wipJobMutex = sync.Mutex{} + // configuration state is stored in this struct type configuration struct { files struct { @@ -76,7 +80,8 @@ func initializeConfig() (config configuration) { config.media.fullsizeMaxHeight = 1080 config.media.videoMaxSize = 640 - config.concurrency = 8 + // TODO adjust based on cores + config.concurrency = 4 return config } @@ -869,8 +874,17 @@ func getGalleryFilenames(sourceFilename string, config configuration) (thumbnail return } +// transformFile takes a transformation job (an image or video) and creates a thumbnail, full-size +// image and a copy of the original func transformFile(thisJob transformationJob, progressBar *pb.ProgressBar, config configuration) { - // TODO add ctrl-C support, signal intercept + // Before we begin work, add all work-in-progress files to wipSlice + // In case the program is killed before we're finished, signalHandler() deletes all the wip files. + // This way, no half-finished files will stay on the hard drive + wipJobMutex.Lock() + wipJobs[thisJob.sourceFilepath] = thisJob + wipJobMutex.Unlock() + + // Do the actual transformation and increment the progress bar if isImageFile(thisJob.filename) { transformImage(thisJob.sourceFilepath, thisJob.fullsizeFilepath, thisJob.thumbnailFilepath, config) } else if isVideoFile(thisJob.filename) { @@ -881,6 +895,10 @@ func transformFile(thisJob transformationJob, progressBar *pb.ProgressBar, confi } createOriginal(thisJob.sourceFilepath, thisJob.originalFilepath) progressBar.Increment() + + wipJobMutex.Lock() + delete(wipJobs, thisJob.sourceFilepath) + wipJobMutex.Unlock() } // This is the main concurrent goroutine that takes care of the parallelisation. A big bunch of them @@ -995,6 +1013,13 @@ func setupSignalHandler() { func signalHandler(signalChan chan os.Signal) { <-signalChan log.Println("Ctrl-C received, cleaning up and aborting...") + wipJobMutex.Lock() + for _, job := range wipJobs { + fmt.Println("cleaning:", job.filename) + os.Remove(job.thumbnailFilepath) + os.Remove(job.fullsizeFilepath) + os.Remove(job.originalFilepath) + } exit(0) } diff --git a/testing/gallery/_fullsize/2021-01-02 23.51.34.mp4 b/testing/gallery/_fullsize/2021-01-02 23.51.34.mp4 deleted file mode 100644 index f9a0926..0000000 Binary files a/testing/gallery/_fullsize/2021-01-02 23.51.34.mp4 and /dev/null differ diff --git a/testing/gallery/_original/2021-01-02 23.51.34.mp4 b/testing/gallery/_original/2021-01-02 23.51.34.mp4 deleted file mode 120000 index c017102..0000000 --- a/testing/gallery/_original/2021-01-02 23.51.34.mp4 +++ /dev/null @@ -1 +0,0 @@ -/home/toni/go/src/github.com/tonimelisma/fastgallery/testing/source/2021-01-02 23.51.34.mp4 \ No newline at end of file diff --git a/testing/gallery/_thumbnail/2021-01-02 23.51.34.jpg b/testing/gallery/_thumbnail/2021-01-02 23.51.34.jpg deleted file mode 100644 index 9e43820..0000000 Binary files a/testing/gallery/_thumbnail/2021-01-02 23.51.34.jpg and /dev/null differ diff --git a/testing/gallery/index.html b/testing/gallery/index.html index 444151c..6954023 100644 --- a/testing/gallery/index.html +++ b/testing/gallery/index.html @@ -42,27 +42,22 @@
- 2021-01-02 23.51.34.mp4 - 2021-01-02 23.51.34.mp4 -
- -
- 2021-02-22 15.36.43-1.heic + 2021-02-22 15.36.43-1.heic 2021-02-22 15.36.43-1.heic
- 2021-02-25 15.40.44.png + 2021-02-25 15.40.44.png 2021-02-25 15.40.44.png
- _DSC9363_DxO.jpg + _DSC9363_DxO.jpg _DSC9363_DxO.jpg
- _DSC9439.jpg + _DSC9439.jpg _DSC9439.jpg
@@ -116,14 +111,6 @@ filename: "2021-01-01 17.11.35.heic" } - , - { - thumbnail: "_thumbnail/2021-01-02 23.51.34.jpg", - fullsize: "_fullsize/2021-01-02 23.51.34.mp4", - original: "_original/2021-01-02 23.51.34.mp4", - filename: "2021-01-02 23.51.34.mp4" - } - , { thumbnail: "_thumbnail/2021-02-22 15.36.43-1.jpg",