Skip to content

Commit

Permalink
Avoid retrieveing media file information if the file is unchanged and…
Browse files Browse the repository at this point in the history
… reset the corresponding fields if the file size is zero
  • Loading branch information
omanikhi committed Feb 26, 2025
1 parent 2addfd1 commit d39425d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 52 deletions.
55 changes: 31 additions & 24 deletions MediaFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,36 @@ public MediaFile(ILogger<MediaContainer> logger,
return;
}

if (Created || Modified)
if
(
(Created || Modified) &&
(
(Size != Original?.Size) ||
(DateModified.TruncateToSeconds() != Original?.DateModified.TruncateToSeconds()) ||
(DateCreated.TruncateToSeconds() != Original?.DateCreated.TruncateToSeconds())
)
)
{
// Retrieve the media specific file information.
GetFileInfo(FullPath);
try
{
Checksum = Size > 0 ? GetFileChecksum(FullPath) : null;
}
catch (Exception e)
{
Logger.LogWarning("Failed To Calculate File Checksum For: {}, Because: {}", FullPath, e.Message);
}
}

if ((Created || Modified) && (Checksum != Original?.Checksum))
{
try
{
GetFileInfo(FullPath, Size);
}
catch (Exception e)
{
Logger.LogWarning("Failed To Retrieve MediaFile Information For: {}, Because: {}", FullPath, e.Message);
}
}

try
Expand Down Expand Up @@ -240,26 +266,6 @@ public MediaFile(ILogger<MediaContainer> logger,
{
Logger.LogWarning("Failed To Generate Thumbnails For: {}, Because: {}", FullPath, e.Message);
}

if
(
(Created || Modified) &&
(
(Size != Original?.Size) ||
(DateModified.TruncateToSeconds() != Original?.DateModified.TruncateToSeconds()) ||
(DateCreated.TruncateToSeconds() != Original?.DateCreated.TruncateToSeconds())
)
)
{
try
{
Checksum = Size > 0 ? GetFileChecksum(FullPath) : null;
}
catch (Exception e)
{
Logger.LogWarning("Failed To Calculate File Checksum For: {}, Because: {}", FullPath, e.Message);
}
}
}

#endregion // Constructors
Expand All @@ -271,7 +277,8 @@ public MediaFile(ILogger<MediaContainer> logger,
/// overridden for each individual type of media file with an implementation specific to that type.
/// </summary>
/// <param name="path">The full path to the physical file.</param>
public virtual void GetFileInfo(string path)
/// <param name="size">The size of the physical file.</param>
public virtual void GetFileInfo(string path, long size)
{
throw new NotImplementedException("This MediaFile does not offer a GetFileInfo() method!");
}
Expand Down
4 changes: 2 additions & 2 deletions Models/MediaContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public class MediaContainer : IEquatable<MediaContainer>
public double? Duration { get; set; }

[SolrField("width")]
public long Width { get; set; }
public long? Width { get; set; }

[SolrField("height")]
public long Height { get; set; }
public long? Height { get; set; }

[SolrField("flags")]
public string[]? Flags { get; set; }
Expand Down
22 changes: 13 additions & 9 deletions PhotoFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class PhotoFile : MediaFile
/// </value>
public DateTime? DateTaken { get; set; }

private long _width = -1;
private long _height = -1;
private long? _width;
private long? _height;

/// <summary>
/// Gets or sets the resolution of the photo file.
Expand All @@ -56,11 +56,8 @@ public ResolutionType Resolution

set
{
if (Resolution != value)
{
_width = value.Width;
_height = value.Height;
}
_width = value.Width;
_height = value.Height;
}
}

Expand Down Expand Up @@ -147,8 +144,16 @@ public PhotoFile(ILogger<MediaContainer> logger,
return null;
}

public override void GetFileInfo(string path)
public override void GetFileInfo(string path, long size)
{
if (size == 0)
{
DateTaken = null;
Resolution = new ResolutionType();

return;
}

var info = GetImageInfo(path);

/*--------------------------------------------------------------------------------
Expand All @@ -157,7 +162,6 @@ public override void GetFileInfo(string path)

if (info != null)
{

Resolution = new ResolutionType(info.Width, info.Height);
}

Expand Down
18 changes: 9 additions & 9 deletions ResolutionType.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
/*
* Copyright © 2024 Travelonium AB
*
*
* This file is part of Arcadeia.
*
*
* Arcadeia is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* Arcadeia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with Arcadeia. If not, see <https://www.gnu.org/licenses/>.
*
*
*/

using System;
Expand All @@ -28,8 +28,8 @@ namespace Arcadeia
/// </summary>
public class ResolutionType : IComparable, IEquatable<ResolutionType>
{
public long Width { get; set; }
public long Height { get; set; }
public long? Width { get; set; }
public long? Height { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="ResolutionType"/> class having been supplied
Expand Down Expand Up @@ -62,7 +62,7 @@ public ResolutionType(string resolution)
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
public ResolutionType(long width, long height)
public ResolutionType(long? width, long? height)
{
Width = width;
Height = height;
Expand Down
21 changes: 13 additions & 8 deletions VideoFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class VideoFile : MediaFile
/// </value>
public double? Duration { get; set; }

private long _width = -1;
private long _height = -1;
private long? _width;
private long? _height;

/// <summary>
/// Gets or sets the resolution of the video file.
Expand All @@ -59,11 +59,8 @@ public ResolutionType Resolution

set
{
if (Resolution != value)
{
_width = value.Width;
_height = value.Height;
}
_width = value.Width;
_height = value.Height;
}
}

Expand Down Expand Up @@ -117,8 +114,16 @@ public VideoFile(ILogger<MediaContainer> logger,

#region Video File Operations

public override void GetFileInfo(string path)
public override void GetFileInfo(string path, long size)
{
if (size == 0)
{
Duration = null;
Resolution = new ResolutionType();

return;
}

string? output = null;
string executable = System.IO.Path.Combine(Settings.CurrentValue.FFmpeg.Path ?? "", "ffprobe" + Platform.Extension.Executable);

Expand Down

0 comments on commit d39425d

Please sign in to comment.