-
Notifications
You must be signed in to change notification settings - Fork 2
/
VideoOptimizedCreator.cs
68 lines (59 loc) · 2.42 KB
/
VideoOptimizedCreator.cs
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
using System;
using System.IO;
using GalleryServerPro.Business.Interfaces;
namespace GalleryServerPro.Business
{
/// <summary>
/// Provides functionality for creating and saving a web-friendly version of a <see cref="Video" /> gallery object.
/// </summary>
public class VideoOptimizedCreator : DisplayObjectCreator
{
/// <summary>
/// Initializes a new instance of the <see cref="VideoOptimizedCreator"/> class.
/// </summary>
/// <param name="galleryObject">The media object.</param>
public VideoOptimizedCreator(IGalleryObject galleryObject)
{
GalleryObject = galleryObject;
}
/// <summary>
/// Generate the file for this display object and save it to the file system. The routine may decide that
/// a file does not need to be generated, usually because it already exists. However, it will always be
/// created if the relevant flag is set on the parent <see cref="IGalleryObject" />. (Example: If
/// <see cref="IGalleryObject.RegenerateThumbnailOnSave" /> = true, the thumbnail file will always be created.) No data is
/// persisted to the data store.
/// </summary>
public override void GenerateAndSaveFile()
{
// If necessary, generate and save the optimized version of the original file.
if (!(IsOptimizedVideoRequired()))
{
return;
}
// Add to queue if an encoder setting exists for this file type.
if (FFmpeg.IsAvailable && MediaConversionQueue.Instance.HasEncoderSetting(GalleryObject))
{
MediaConversionQueue.Instance.Add(GalleryObject, MediaQueueItemConversionType.CreateOptimized);
MediaConversionQueue.Instance.Process();
}
}
private bool IsOptimizedVideoRequired()
{
if (GalleryObject.IsNew || IsInQueue())
return false;
var optFileIsMissing = IsOptimizedFileMissing();
var overwriteFlag = GalleryObject.RegenerateOptimizedOnSave;
return (optFileIsMissing || overwriteFlag);
}
private bool IsInQueue()
{
return MediaConversionQueue.Instance.IsWaitingInQueueOrProcessing(GalleryObject.Id, MediaQueueItemConversionType.CreateOptimized);
}
private bool IsOptimizedFileMissing()
{
// We need an optimized file if the opt. and original file names are the same or if the file doesn't exist.
var optFileSameAsOriginal = String.Equals(GalleryObject.Optimized.FileName, GalleryObject.Original.FileName, StringComparison.OrdinalIgnoreCase);
return (optFileSameAsOriginal || !File.Exists(GalleryObject.Optimized.FileNamePhysicalPath));
}
}
}