/
media.go
948 lines (868 loc) · 30.1 KB
/
media.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
package main
import (
"bytes"
"fmt"
"image"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/GeertJohan/go.rice"
"github.com/disintegration/imaging"
"github.com/midstar/llog"
"github.com/rwcarlsen/goexif/exif"
)
var imgExtensions = [...]string{".png", ".jpg", ".jpeg", ".tif", ".tiff", ".gif"}
var vidExtensions = [...]string{".avi", ".mov", ".vid", ".mkv", ".mp4"}
// Media represents the media including its base path
type Media struct {
mediaPath string // Top level path for media files
cachepath string // Top level path for thumbnails
enableThumbCache bool // Generate thumbnails
autoRotate bool // Rotate JPEG files when needed
enablePreview bool // Resize images before provide to client
previewMaxSide int // Maximum width or hight of preview image
enableCacheCleanup bool // Enable cleanup of cache area
box *rice.Box // For icons
preCacheInProgress bool // True if thumbnail/preview generation in progress
watcher *Watcher // The media watcher
}
// File represents a folder or any other file
type File struct {
Type string // folder, image or video
Name string
Path string // Including Name. Always using / (even on Windows)
}
// createMedia creates a new media. If thumb cache is enabled the path is
// created when needed.
func createMedia(box *rice.Box, mediaPath string, cachepath string, enableThumbCache,
genThumbsOnStartup, genThumbsOnAdd, autoRotate, enablePreview bool,
previewMaxSide int, genPreviewOnStartup, genPreviewOnAdd, enabledCacheCleanup bool) *Media {
llog.Info("Media path: %s", mediaPath)
if enableThumbCache || enablePreview {
directory := filepath.Dir(cachepath)
err := os.MkdirAll(directory, os.ModePerm)
if err != nil {
llog.Warn("Unable to create cache path %s. Reason: %s", cachepath, err)
llog.Info("Thumbnail and preview cache will be disabled")
enableThumbCache = false
enablePreview = false
} else {
llog.Info("Cache path: %s", cachepath)
}
} else {
llog.Info("Cache disabled")
}
llog.Info("JPEG auto rotate: %t", autoRotate)
llog.Info("Image preview: %t (max width/height %d px)", enablePreview, previewMaxSide)
media := &Media{mediaPath: filepath.ToSlash(filepath.Clean(mediaPath)),
cachepath: filepath.ToSlash(filepath.Clean(cachepath)),
enableThumbCache: enableThumbCache,
autoRotate: autoRotate,
enablePreview: enablePreview,
previewMaxSide: previewMaxSide,
enableCacheCleanup: enabledCacheCleanup,
box: box,
preCacheInProgress: false}
llog.Info("Video thumbnails supported (ffmpeg installed): %v", media.videoThumbnailSupport())
if enableThumbCache && genThumbsOnStartup || enablePreview && genPreviewOnStartup {
go media.generateAllCache(enableThumbCache && genThumbsOnStartup, enablePreview && genPreviewOnStartup)
}
if enableThumbCache && genThumbsOnAdd || enablePreview && genPreviewOnAdd {
media.watcher = createWatcher(media, enableThumbCache && genThumbsOnAdd, enablePreview && genPreviewOnAdd)
go media.watcher.startWatcher()
}
return media
}
// getFullPath returns the full path from an absolute base
// path and a relative path. Returns error on security hacks,
// i.e. when someone tries to access ../../../ for example to
// get files that are not within configured base path.
//
// Always returning front slashes / as path separator
func (m *Media) getFullPath(basePath, relativePath string) (string, error) {
fullPath := filepath.ToSlash(filepath.Join(basePath, relativePath))
diffPath, err := filepath.Rel(basePath, fullPath)
diffPath = filepath.ToSlash(diffPath)
if err != nil || strings.HasPrefix(diffPath, "../") {
return m.mediaPath, fmt.Errorf("Hacker attack. Someone tries to access: %s", fullPath)
}
return fullPath, nil
}
// getFullMediaPath returns the full path of the provided path, i.e:
// media path + relative path.
func (m *Media) getFullMediaPath(relativePath string) (string, error) {
return m.getFullPath(m.mediaPath, relativePath)
}
// getFullCachePath returns the full path of the provided path, i.e:
// thumb path + relative path.
func (m *Media) getFullCachePath(relativePath string) (string, error) {
return m.getFullPath(m.cachepath, relativePath)
}
// getRelativePath returns the relative path from an absolute base
// path and a full path path. Returns error if the base path is
// not in the full path.
//
// Always returning front slashes / as path separator
func (m *Media) getRelativePath(basePath, fullPath string) (string, error) {
relativePath, err := filepath.Rel(basePath, fullPath)
if err == nil {
relativePathSlash := filepath.ToSlash(relativePath)
if strings.HasPrefix(relativePathSlash, "../") {
return "", fmt.Errorf("%s is not a sub-path of %s", fullPath, basePath)
}
return relativePathSlash, nil
}
return "", err
}
// getRelativeMediaPath returns the relative media path of the provided path, i.e:
// full path - media path.
func (m *Media) getRelativeMediaPath(fullPath string) (string, error) {
return m.getRelativePath(m.mediaPath, fullPath)
}
// getFiles returns a slice of File's sorted on file name
func (m *Media) getFiles(relativePath string) ([]File, error) {
//var files []File
files := make([]File, 0, 500)
fullPath, err := m.getFullMediaPath(relativePath)
if err != nil {
return files, err
}
fileInfos, err := ioutil.ReadDir(fullPath)
if err != nil {
return files, err
}
for _, fileInfo := range fileInfos {
fileType := ""
if fileInfo.IsDir() || fileInfo.Mode()&os.ModeSymlink != 0 {
fileType = "folder"
} else {
fileType = m.getFileType(fileInfo.Name())
}
// Only add directories, videos and images
if fileType != "" {
// Use path with / slash
pathOriginal := filepath.Join(relativePath, fileInfo.Name())
pathNew := filepath.ToSlash(pathOriginal)
file := File{
Type: fileType,
Name: fileInfo.Name(),
Path: pathNew}
files = append(files, file)
} else {
llog.Debug("getFiles - omitting: %s", fileInfo.Name())
}
}
return files, nil
}
// getFileType returns "video" for video files and "image" for image files.
// For all other files (including folders) "" is returned.
// relativeFileName can also include an absolute or relative path.
func (m *Media) getFileType(relativeFileName string) string {
// Check if this is an image
if m.isImage(relativeFileName) {
return "image"
}
// Check if this is a video
if m.isVideo(relativeFileName) {
return "video"
}
return "" // Not a video nor an image
}
func (m *Media) isImage(pathAndFile string) bool {
extension := filepath.Ext(pathAndFile)
for _, imgExtension := range imgExtensions {
if strings.EqualFold(extension, imgExtension) {
return true
}
}
return false
}
func (m *Media) isVideo(pathAndFile string) bool {
extension := filepath.Ext(pathAndFile)
for _, vidExtension := range vidExtensions {
if strings.EqualFold(extension, vidExtension) {
return true
}
}
return false
}
func (m *Media) isJPEG(pathAndFile string) bool {
extension := filepath.Ext(pathAndFile)
if strings.EqualFold(extension, ".jpg") == false &&
strings.EqualFold(extension, ".jpeg") == false {
return false
}
return true
}
func (m *Media) extractEXIF(relativeFilePath string) *exif.Exif {
fullFilePath, err := m.getFullMediaPath(relativeFilePath)
if err != nil {
llog.Info("Unable to get full media path for %s\n", relativeFilePath)
return nil
}
if !m.isJPEG(fullFilePath) {
return nil // Only JPEG has EXIF
}
efile, err := os.Open(fullFilePath)
if err != nil {
llog.Warn("Could not open file for EXIF decoding. File: %s reason: %s\n", fullFilePath, err)
return nil
}
defer efile.Close()
ex, err := exif.Decode(efile)
if err != nil {
llog.Debug("No EXIF. file %s reason: %s\n", fullFilePath, err)
return nil
}
return ex
}
// isRotationNeeded returns true if the file needs to be rotated.
// It finds this out by reading the EXIF rotation information
// in the file.
// If Media.autoRotate is false this function will always return
// false.
func (m *Media) isRotationNeeded(relativeFilePath string) bool {
if m.autoRotate == false {
return false
}
ex := m.extractEXIF(relativeFilePath)
if ex == nil {
return false // No EXIF info exist
}
orientTag, _ := ex.Get(exif.Orientation)
if orientTag == nil {
return false // No Orientation
}
orientInt, _ := orientTag.Int(0)
if orientInt > 1 && orientInt < 9 {
return true // Rotation is needed
}
return false
}
// rotateAndWrite opens and rotates a JPG/JPEG file according to
// EXIF rotation information. Then it writes the rotated image
// to the io.Writer. NOTE! This process requires Decoding and
// encoding of the image which takes a LOT of time (2-3 sec).
// Check if image needs rotation with isRotationNeeded first.
func (m *Media) rotateAndWrite(w io.Writer, relativeFilePath string) error {
fullPath, err := m.getFullMediaPath(relativeFilePath)
if err != nil {
return err
}
img, err := imaging.Open(fullPath, imaging.AutoOrientation(true))
if err != nil {
return err
}
err = imaging.Encode(w, img, imaging.JPEG)
if err != nil {
return err
}
return nil
}
// writeEXIFThumbnail extracts the EXIF thumbnail from a JPEG file
// and rotates it when needed (based on the EXIF orientation tag).
// Returns err if no thumbnail exist.
func (m *Media) writeEXIFThumbnail(w io.Writer, relativeFilePath string) error {
ex := m.extractEXIF(relativeFilePath)
if ex == nil {
return fmt.Errorf("No EXIF info for %s", relativeFilePath)
}
thumbBytes, err := ex.JpegThumbnail()
if err != nil {
return fmt.Errorf("No EXIF thumbnail for %s", relativeFilePath)
}
orientTag, _ := ex.Get(exif.Orientation)
if orientTag == nil {
// No Orientation assume no rotation needed
w.Write(thumbBytes)
return nil
}
orientInt, _ := orientTag.Int(0)
if orientInt > 1 && orientInt < 9 {
// Rotation is needed
img, err := imaging.Decode(bytes.NewReader(thumbBytes))
if err != nil {
llog.Warn("Unable to decode EXIF thumbnail for %s", relativeFilePath)
w.Write(thumbBytes)
return nil
}
var outImg *image.NRGBA
switch orientInt {
case 2:
outImg = imaging.FlipV(img)
case 3:
outImg = imaging.Rotate180(img)
case 4:
outImg = imaging.Rotate180(imaging.FlipV(img))
case 5:
outImg = imaging.Rotate270(imaging.FlipV(img))
case 6:
outImg = imaging.Rotate270(img)
case 7:
outImg = imaging.Rotate90(imaging.FlipV(img))
case 8:
outImg = imaging.Rotate90(img)
}
imaging.Encode(w, outImg, imaging.JPEG)
} else {
// No rotation is needed
w.Write(thumbBytes)
}
return nil
}
// thumbnailPath returns the absolute thumbnail file path from a
// media path. Thumbnails are always stored in JPEG format (.jpg
// extension) and starts with '_'.
// Returns error if the media path is invalid.
func (m *Media) thumbnailPath(relativeMediaPath string) (string, error) {
path, file := filepath.Split(relativeMediaPath)
if !m.isJPEG(file) {
// Replace extension with .jpg
ext := filepath.Ext(file)
if ext == "" {
return "", fmt.Errorf("File has no extension: %s", file)
}
file = strings.Replace(file, ext, ".jpg", -1)
}
file = "_" + file
relativeThumbnailPath := filepath.Join(path, file)
return m.getFullCachePath(relativeThumbnailPath)
}
// errorIndicationPath returns the file path with the extension
// replaced with err.
func (m *Media) errorIndicationPath(anyPath string) string {
path, file := filepath.Split(anyPath)
ext := filepath.Ext(file)
file = strings.Replace(file, ext, ".err.txt", -1)
return filepath.Join(path, file)
}
// generateErrorIndication creates a text file including the error reason.
func (m *Media) generateErrorIndicationFile(errorIndicationFile string, err error) {
llog.Warn("%s", err)
errorFile, err2 := os.Create(errorIndicationFile)
if err2 == nil {
defer errorFile.Close()
errorFile.WriteString(err.Error())
llog.Info("Created: %s", errorIndicationFile)
} else {
llog.Warn("Unable to create %s. Reason: %s", errorIndicationFile, err2)
}
}
// generateImageThumbnail generates a thumbnail from any of the supported
// images. Will create necessary subdirectories in the thumbpath.
func (m *Media) generateImageThumbnail(fullMediaPath, fullThumbPath string) error {
img, err := imaging.Open(fullMediaPath, imaging.AutoOrientation(true))
if err != nil {
return fmt.Errorf("Unable to open image %s. Reason: %s", fullMediaPath, err)
}
thumbImg := imaging.Thumbnail(img, 256, 256, imaging.Box)
// Create subdirectories if needed
directory := filepath.Dir(fullThumbPath)
err = os.MkdirAll(directory, os.ModePerm)
if err != nil {
return fmt.Errorf("Unable to create directories in %s for creating thumbnail. Reason %s", fullThumbPath, err)
}
// Write thumbnail to file
outFile, err := os.Create(fullThumbPath)
if err != nil {
return fmt.Errorf("Unable to open %s for creating thumbnail. Reason %s", fullThumbPath, err)
}
defer outFile.Close()
err = imaging.Encode(outFile, thumbImg, imaging.JPEG)
return err
}
// generateTumbnail generates a thumbnail for an image or video
// and returns the file name of the thumbnail. If a thumbnail already
// exist the file name will be returned.
func (m *Media) generateThumbnail(relativeFilePath string) (string, error) {
thumbFileName, err := m.thumbnailPath(relativeFilePath)
if err != nil {
llog.Warn("%s", err)
return "", err
}
_, err = os.Stat(thumbFileName) // Check if file exist
if err == nil {
return thumbFileName, nil // Thumb already generated
}
errorIndicationFile := m.errorIndicationPath(thumbFileName)
_, err = os.Stat(errorIndicationFile) // Check if file exist
if err == nil {
// File has failed to be generated before, don't bother
// trying to re-generate it.
msg := fmt.Sprintf("Skipping generate thumbnail for %s since it has failed before.",
relativeFilePath)
llog.Trace(msg)
return "", fmt.Errorf(msg)
}
// No thumb exist. Create it
llog.Info("Creating new thumbnail for %s", relativeFilePath)
startTime := time.Now().UnixNano()
fullMediaPath, err := m.getFullMediaPath(relativeFilePath)
if err != nil {
llog.Warn("%s", err)
return "", err
}
if m.isVideo(fullMediaPath) {
err = m.generateVideoThumbnail(fullMediaPath, thumbFileName)
} else {
err = m.generateImageThumbnail(fullMediaPath, thumbFileName)
}
if err != nil {
// To avoid generate the file again, create an error indication file
m.generateErrorIndicationFile(errorIndicationFile, err)
return "", err
}
deltaTime := (time.Now().UnixNano() - startTime) / int64(time.Millisecond)
llog.Info("Thumbnail done for %s (conversion time: %d ms)",
relativeFilePath, deltaTime)
return thumbFileName, nil
}
// writeThumbnail writes thumbnail for media to w.
//
// It has following sequence/priority:
// 1. Write embedded EXIF thumbnail if it exist (only JPEG)
// 2. Write a cached thumbnail file exist in cachepath
// 3. Generate a thumbnail to cache and write
// 4. If all above fails return error
func (m *Media) writeThumbnail(w io.Writer, relativeFilePath string) error {
if !m.isImage(relativeFilePath) && !m.isVideo(relativeFilePath) {
return fmt.Errorf("not a supported media type")
}
if m.writeEXIFThumbnail(w, relativeFilePath) == nil {
return nil
}
if !m.enableThumbCache {
return fmt.Errorf("Thumbnail cache disabled")
}
// No EXIF, check thumb cache (and generate if necessary)
thumbFileName, err := m.generateThumbnail(relativeFilePath)
if err != nil {
return err // Logging handled in generateThumbnail
}
thumbFile, err := os.Open(thumbFileName)
if err != nil {
return err
}
defer thumbFile.Close()
_, err = io.Copy(w, thumbFile)
if err != nil {
return err
}
return nil
}
// For testing purposes
var ffmpegCmd = "ffmpeg"
// videoThumbnailSupport returns true if ffmpeg is installed, and thus
// video thumbnails is supported
func (m *Media) videoThumbnailSupport() bool {
_, err := exec.LookPath(ffmpegCmd)
return err == nil
}
// generateVideoThumbnail generates a thumbnail from any of the supported
// videos. Will create necessary subdirectories in the thumbpath.
func (m *Media) generateVideoThumbnail(fullMediaPath, fullThumbPath string) error {
// The temporary file for the screenshot
screenShot := fullThumbPath + ".sh.jpg"
// Extract the screenshot
err := m.extractVideoScreenshot(fullMediaPath, screenShot)
if err != nil {
return err
}
defer os.Remove(screenShot) // Remove temporary file
// Generate thumbnail from the screenshot
img, err := imaging.Open(screenShot, imaging.AutoOrientation(true))
if err != nil {
return fmt.Errorf("Unable to open screenshot image %s. Reason: %s", screenShot, err)
}
thumbImg := imaging.Thumbnail(img, 256, 256, imaging.Box)
// Add small video icon i upper right corner to indicate that this is
// a video
iconVideoImg, err := m.getVideoIcon()
if err != nil {
return err
}
thumbImg = imaging.Overlay(thumbImg, iconVideoImg, image.Pt(155, 11), 1.0)
// Write thumbnail to file
outFile, err := os.Create(fullThumbPath)
if err != nil {
return fmt.Errorf("Unable to open %s for creating thumbnail. Reason %s", fullThumbPath, err)
}
defer outFile.Close()
err = imaging.Encode(outFile, thumbImg, imaging.JPEG)
return err
}
// Cache to avoid regenerate icon each time (do it once)
var videoIcon image.Image
func (m *Media) getVideoIcon() (image.Image, error) {
if videoIcon != nil {
// To avoid re-generate
return videoIcon, nil
}
var err error
videoIconBytes, _ := m.box.Bytes("icon_video.png")
videoIcon, err = imaging.Decode(bytes.NewReader(videoIconBytes))
if err != nil {
return nil, err
}
videoIcon = imaging.Resize(videoIcon, 90, 90, imaging.Box)
return videoIcon, nil
}
// extractVideoScreenshot extracts a screenshot from a video using external
// ffmpeg software. Will create necessary directories in the outFilePath
func (m *Media) extractVideoScreenshot(inFilePath, outFilePath string) error {
if !m.videoThumbnailSupport() {
return fmt.Errorf("video thumbnails not supported. ffmpeg not installed")
}
// Create subdirectories if needed
directory := filepath.Dir(outFilePath)
err := os.MkdirAll(directory, os.ModePerm)
if err != nil {
return fmt.Errorf("Unable to create directories in %s for extracting screenshot. Reason %s", outFilePath, err)
}
// Define argments for ffmpeg
ffmpegArgs := []string{
"-i",
inFilePath,
"-ss",
"00:00:05", // 5 seconds into movie
"-vframes",
"1",
outFilePath}
var stdout bytes.Buffer
var stderr bytes.Buffer
//cmd := exec.Command(ffmpegCmd, ffmpegArg)
cmd := exec.Command(ffmpegCmd, ffmpegArgs...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
_, outFileErr := os.Stat(outFilePath)
if err != nil || outFileErr != nil {
return fmt.Errorf("%s %s\nStdout: %s\nStderr: %s",
ffmpegCmd, strings.Join(ffmpegArgs, " "), stdout.String(), stderr.String())
}
return nil
}
// getImageWidthAndHeight returns the width and height of an image.
// Returns error if the width and height could not be determined.
func (m *Media) getImageWidthAndHeight(fullMediaPath string) (int, int, error) {
img, err := imaging.Open(fullMediaPath)
if err != nil {
return 0, 0, fmt.Errorf("Unable to open image %s. Reason: %s", fullMediaPath, err)
}
return img.Bounds().Dx(), img.Bounds().Dy(), nil
}
// previewPath returns the absolute preview file path from a
// media path. Previews are always stored in JPEG format (.jpg
// extension) and starts with 'view_'.
// Returns error if the media path is invalid.
func (m *Media) previewPath(relativeMediaPath string) (string, error) {
path, file := filepath.Split(relativeMediaPath)
if !m.isJPEG(file) {
// Replace extension with .jpg
ext := filepath.Ext(file)
if ext == "" {
return "", fmt.Errorf("File has no extension: %s", file)
}
file = strings.Replace(file, ext, ".jpg", -1)
}
file = "view_" + file
relativePreviewPath := filepath.Join(path, file)
return m.getFullCachePath(relativePreviewPath)
}
// generateImagePreview generates a preview from any of the supported
// images. Will create necessary subdirectories in the PreviewPath.
func (m *Media) generateImagePreview(fullMediaPath, fullPreviewPath string) error {
img, err := imaging.Open(fullMediaPath, imaging.AutoOrientation(true))
if err != nil {
return fmt.Errorf("Unable to open image %s. Reason: %s", fullMediaPath, err)
}
previewImg := imaging.Fit(img, m.previewMaxSide, m.previewMaxSide, imaging.Box)
// Create subdirectories if needed
directory := filepath.Dir(fullPreviewPath)
err = os.MkdirAll(directory, os.ModePerm)
if err != nil {
return fmt.Errorf("Unable to create directories in %s for creating preview. Reason %s", fullPreviewPath, err)
}
// Write thumbnail to file
outFile, err := os.Create(fullPreviewPath)
if err != nil {
return fmt.Errorf("Unable to open %s for creating preview. Reason %s", fullPreviewPath, err)
}
defer outFile.Close()
err = imaging.Encode(outFile, previewImg, imaging.JPEG)
return err
}
// generatePreview generates a preview image and returns the file name of the
// preview. If a preview file already exist the file name will be returned.
func (m *Media) generatePreview(relativeFilePath string) (string, bool, error) {
previewFileName, err := m.previewPath(relativeFilePath)
if err != nil {
llog.Warn("%s", err)
return "", false, err
}
_, err = os.Stat(previewFileName) // Check if file exist
if err == nil {
return previewFileName, false, nil // Preview already generated
}
errorIndicationFile := m.errorIndicationPath(previewFileName)
_, err = os.Stat(errorIndicationFile) // Check if file exist
if err == nil {
// File has failed to be generated before, don't bother
// trying to re-generate it.
msg := fmt.Sprintf("Skipping generate preview for %s since it has failed before.",
relativeFilePath)
llog.Trace(msg)
return "", false, fmt.Errorf(msg)
}
fullMediaPath, err := m.getFullMediaPath(relativeFilePath)
if err != nil {
llog.Warn("%s", err)
return "", false, err
}
width, height, err := m.getImageWidthAndHeight(fullMediaPath)
if err != nil {
// To avoid generate the file again, create an error indication file
m.generateErrorIndicationFile(errorIndicationFile, err)
return "", false, err
}
if width <= m.previewMaxSide && height <= m.previewMaxSide {
msg := fmt.Sprintf("Image %s too small to generate preview", relativeFilePath)
llog.Trace(msg)
return "", true, fmt.Errorf(msg)
}
// No preview exist. Create it
llog.Info("Creating new preview file for %s", relativeFilePath)
startTime := time.Now().UnixNano()
err = m.generateImagePreview(fullMediaPath, previewFileName)
if err != nil {
// To avoid generate the file again, create an error indication file
m.generateErrorIndicationFile(errorIndicationFile, err)
return "", false, err
}
deltaTime := (time.Now().UnixNano() - startTime) / int64(time.Millisecond)
llog.Info("Preview done for %s (conversion time: %d ms)",
relativeFilePath, deltaTime)
return previewFileName, false, nil
}
// writePreview writes preview image for media to w.
//
// It has following sequence/priority:
// 1. Write a cached preview file exist
// 2. Generate a preview in cache and write
// 3. If all above fails return error
func (m *Media) writePreview(w io.Writer, relativeFilePath string) error {
if !m.isImage(relativeFilePath) {
return fmt.Errorf("only images support preview")
}
if !m.enablePreview {
return fmt.Errorf("Preview disabled")
}
// Check preview cache (and generate if necessary)
previewFileName, _, err := m.generatePreview(relativeFilePath)
if err != nil {
return err // Logging handled in generatePreview
}
previewFile, err := os.Open(previewFileName)
if err != nil {
return err
}
defer previewFile.Close()
_, err = io.Copy(w, previewFile)
if err != nil {
return err
}
return nil
}
// PreCacheStatistics statistics results from generateCache
type PreCacheStatistics struct {
NbrOfFolders int
NbrOfImages int
NbrOfVideos int
NbrOfExif int
NbrOfImageThumb int
NbrOfVideoThumb int
NbrOfImagePreview int
NbrOfFailedFolders int // I.e. unable to list contents of folder
NbrOfFailedImageThumb int
NbrOfFailedVideoThumb int
NbrOfFailedImagePreview int
NbrOfSmallImages int // Don't require any preview
NbrRemovedCacheFiles int
}
func (m *Media) isPreCacheInProgress() bool {
return m.preCacheInProgress
}
// generateCache recursively (optional) goes through all files
// relativePath and its subdirectories and generates thumbnails and
// previews for these. If relativePath is "" it means generate for all files.
func (m *Media) generateCache(relativePath string, recursive, thumbnails, preview bool) *PreCacheStatistics {
prevProgress := m.preCacheInProgress
m.preCacheInProgress = true
defer func() { m.preCacheInProgress = prevProgress }()
stat := PreCacheStatistics{}
files, err := m.getFiles(relativePath)
if err != nil {
stat.NbrOfFailedFolders = 1
return &stat
}
for _, file := range files {
if file.Type == "folder" {
if recursive {
stat.NbrOfFolders++
newStat := m.generateCache(file.Path, true, thumbnails, preview) // Recursive
stat.NbrOfFolders += newStat.NbrOfFolders
stat.NbrOfImages += newStat.NbrOfImages
stat.NbrOfVideos += newStat.NbrOfVideos
stat.NbrOfExif += newStat.NbrOfExif
stat.NbrOfImageThumb += newStat.NbrOfImageThumb
stat.NbrOfVideoThumb += newStat.NbrOfVideoThumb
stat.NbrOfImagePreview += newStat.NbrOfImagePreview
stat.NbrOfFailedFolders += newStat.NbrOfFailedFolders
stat.NbrOfFailedImageThumb += newStat.NbrOfFailedImageThumb
stat.NbrOfFailedVideoThumb += newStat.NbrOfFailedVideoThumb
stat.NbrOfFailedImagePreview += newStat.NbrOfFailedImagePreview
stat.NbrOfSmallImages += newStat.NbrOfSmallImages
stat.NbrRemovedCacheFiles += newStat.NbrRemovedCacheFiles
}
} else {
if file.Type == "image" {
stat.NbrOfImages++
} else if file.Type == "video" {
stat.NbrOfVideos++
}
// Check if file has EXIF thumbnail
hasExifThumb := false
ex := m.extractEXIF(file.Path)
if ex != nil {
_, err := ex.JpegThumbnail()
if err == nil {
// Media has EXIF thumbnail
stat.NbrOfExif++
hasExifThumb = true
}
}
if thumbnails && !hasExifThumb {
// Generate new thumbnail
_, err = m.generateThumbnail(file.Path)
if err != nil {
if file.Type == "image" {
stat.NbrOfFailedImageThumb++
} else if file.Type == "video" {
stat.NbrOfFailedVideoThumb++
}
} else {
if file.Type == "image" {
stat.NbrOfImageThumb++
} else if file.Type == "video" {
stat.NbrOfVideoThumb++
}
}
}
if preview && file.Type == "image" {
// Generate new preview
_, tooSmall, err := m.generatePreview(file.Path)
if err != nil {
if tooSmall {
stat.NbrOfSmallImages++
} else {
stat.NbrOfFailedImagePreview++
}
} else {
stat.NbrOfImagePreview++
}
}
}
}
if m.enableCacheCleanup {
stat.NbrRemovedCacheFiles += m.cleanupCache(relativePath, files)
}
return &stat
}
// generateAllCache goes through all files in the media path
// and generates thumbnails/preview for these
func (m *Media) generateAllCache(thumbnails, preview bool) {
llog.Info("Pre-generating cache (thumbnails: %t, preview: %t)", thumbnails, preview)
startTime := time.Now().UnixNano()
stat := m.generateCache("", true, thumbnails, preview)
deltaTime := (time.Now().UnixNano() - startTime) / int64(time.Second)
minutes := int(deltaTime / 60)
seconds := int(deltaTime) - minutes*60
llog.Info(`Generating cache took %d minutes and %d seconds
Number of folders: %d
Number of images: %d
Number of videos: %d
Number of images with embedded EXIF: %d
Number of generated image thumbnails: %d
Number of generated video thumbnails: %d
Number of generated image previews: %d
Number of failed folders: %d
Number of failed image thumbnails: %d
Number of failed video thumbnails: %d
Number of failed image previews: %d
Number of small images not require preview: %d
Number of removed cache files: %d`, minutes, seconds, stat.NbrOfFolders, stat.NbrOfImages,
stat.NbrOfVideos, stat.NbrOfExif, stat.NbrOfImageThumb, stat.NbrOfVideoThumb, stat.NbrOfImagePreview,
stat.NbrOfFailedFolders, stat.NbrOfFailedImageThumb, stat.NbrOfFailedVideoThumb,
stat.NbrOfFailedImagePreview, stat.NbrOfSmallImages, stat.NbrRemovedCacheFiles)
}
// cleanupCache removes all files and directories in the cache directory
// which don't have any corresponding media file.
// relativePath relative path where to clean up cache files.
// expectedMediaFiles are all files, including directories that are allowed
// as thumbs, preview or error files in the cache.
// Returns number of removed files and directories
func (m *Media) cleanupCache(relativePath string, expectedMediaFiles []File) int {
fullCachePath, _ := m.getFullCachePath(relativePath)
llog.Debug("Cleaning up directory: %s", fullCachePath)
// Figure possible directories, thumb, preview and error file names
cacheFileNames := make([]string, 0, len(expectedMediaFiles)*5)
for _, file := range expectedMediaFiles {
_, fileName := filepath.Split(file.Name)
if file.Type == "folder" {
cacheFileNames = append(cacheFileNames, fileName)
} else {
thumbName, err := m.thumbnailPath(fileName)
if err == nil {
_, thumbName = filepath.Split(thumbName)
cacheFileNames = append(cacheFileNames, thumbName)
errorIndicationName := m.errorIndicationPath(thumbName)
_, errorIndicationName = filepath.Split(errorIndicationName)
cacheFileNames = append(cacheFileNames, errorIndicationName)
}
previewName, err := m.previewPath(fileName)
if err == nil {
_, previewName = filepath.Split(previewName)
cacheFileNames = append(cacheFileNames, previewName)
errorIndicationName := m.errorIndicationPath(previewName)
_, errorIndicationName = filepath.Split(errorIndicationName)
cacheFileNames = append(cacheFileNames, errorIndicationName)
}
}
}
// Compare the files in cache path with expected files
fileInfos, _ := ioutil.ReadDir(fullCachePath)
nbrRemovedFiles := 0
for _, fileInfo := range fileInfos {
if contains(cacheFileNames, fileInfo.Name()) == false {
filePath := filepath.Join(fullCachePath, fileInfo.Name())
llog.Debug("Removing %s", filePath)
os.RemoveAll(filePath)
nbrRemovedFiles++
}
}
return nbrRemovedFiles
}
// contains is a helper function to find a string within
// a slice of multiple strings
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}