Skip to content

Commit

Permalink
Fixes for FileDownloadLocation not being cleaned on disposal
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigHawker committed Mar 23, 2023
1 parent 6f84c74 commit 65a53ed
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 162 deletions.
323 changes: 161 additions & 162 deletions MFilesAPI.Extensions/Files/Downloading/FileDownloadLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,96 +7,96 @@

namespace MFilesAPI.Extensions
{
/// <summary>
/// A location for temporary file downloads to be held.
/// </summary>
public class FileDownloadLocation
: DisposableBase
{
/// <summary>
/// The <see cref="DirectoryInfo"/> into which temporary files are placed.
/// </summary>
public DirectoryInfo Directory { get; protected set; }

/// <summary>
/// If true, the <see cref="Directory"/> will be cleaned of files when this object is disposed.
/// </summary>
public bool CleanDirectoryOnDisposal { get; set; }

/// <summary>
/// The default file extension for temporary files, if no file extension is provided/available.
/// </summary>
public const string DefaultFileExtension = ".tmp";

/// <summary>
/// Whether this <see cref="FileDownloadLocation"/> instance created the
/// directory or not. If true, disposal should delete it too.
/// </summary>
protected bool CreatedDirectoryForThisUsage { get; }

/// <summary>
/// Creates a <see cref="FileDownloadLocation"/> pointing at the provided <paramref name="directory"/>.
/// </summary>
/// <param name="directory">The location for the files to be downloaded to.</param>
protected FileDownloadLocation
(
DirectoryInfo directory
)
{
// Sanity.
this.Directory = directory ?? throw new ArgumentNullException(nameof(directory));

// If the location does not exist then create it.
if (false == this.Directory.Exists)
{
this.CreatedDirectoryForThisUsage = true;
this.Directory.Create();
}
}

/// <summary>
/// Creates a <see cref="FileDownloadLocation"/>.
/// Creates a folder named <paramref name="temporaryPath"/> within <paramref name="folderName"/> to hold the downloaded files.
/// </summary>
/// <param name="temporaryPath">The parent temporary path.</param>
/// <param name="folderName">The folder name to create in <paramref name="temporaryPath"/>. All files will be saved into this subfolder.</param>
public FileDownloadLocation
(
string temporaryPath,
string folderName
)
: this
(
new DirectoryInfo(System.IO.Path.Combine(temporaryPath, folderName))
)
{
}

/// <summary>
/// Creates a <see cref="FileDownloadLocation"/> pointing at the system temporary area.
/// Will create a subfolder called the application domain name.
/// </summary>
public FileDownloadLocation()
: this
(
System.IO.Path.GetTempPath(),
System.Reflection.Assembly.GetCallingAssembly().GetName().Name
)
{
}

/// <summary>
/// Deletes all temporary files from this location.
/// </summary>
/// <param name="suppressErrors">If true then errors deleting files are not thrown.</param>
public virtual void CleanTemporaryFiles
(
bool suppressErrors = true
)
/// <summary>
/// A location for temporary file downloads to be held.
/// </summary>
public class FileDownloadLocation
: DisposableBase
{
/// <summary>
/// The <see cref="DirectoryInfo"/> into which temporary files are placed.
/// </summary>
public DirectoryInfo Directory { get; protected set; }

/// <summary>
/// If true, the <see cref="Directory"/> will be cleaned of files when this object is disposed.
/// </summary>
public bool CleanDirectoryOnDisposal { get; set; } = true;

/// <summary>
/// The default file extension for temporary files, if no file extension is provided/available.
/// </summary>
public const string DefaultFileExtension = ".tmp";

/// <summary>
/// Whether this <see cref="FileDownloadLocation"/> instance created the
/// directory or not. If true, disposal should delete it too.
/// </summary>
protected bool CreatedDirectoryForThisUsage { get; }

/// <summary>
/// Creates a <see cref="FileDownloadLocation"/> pointing at the provided <paramref name="directory"/>.
/// </summary>
/// <param name="directory">The location for the files to be downloaded to.</param>
protected FileDownloadLocation
(
DirectoryInfo directory
)
{
// If we created the directory then remove it all.
if(this.CreatedDirectoryForThisUsage)
{
// Sanity.
this.Directory = directory ?? throw new ArgumentNullException(nameof(directory));

// If the location does not exist then create it.
if (false == this.Directory.Exists)
{
this.CreatedDirectoryForThisUsage = true;
this.Directory.Create();
}
}

/// <summary>
/// Creates a <see cref="FileDownloadLocation"/>.
/// Creates a folder named <paramref name="temporaryPath"/> within <paramref name="folderName"/> to hold the downloaded files.
/// </summary>
/// <param name="temporaryPath">The parent temporary path.</param>
/// <param name="folderName">The folder name to create in <paramref name="temporaryPath"/>. All files will be saved into this subfolder.</param>
public FileDownloadLocation
(
string temporaryPath,
string folderName
)
: this
(
new DirectoryInfo(System.IO.Path.Combine(temporaryPath, folderName))
)
{
}

/// <summary>
/// Creates a <see cref="FileDownloadLocation"/> pointing at the system temporary area.
/// Will create a subfolder called the application domain name.
/// </summary>
public FileDownloadLocation()
: this
(
System.IO.Path.GetTempPath(),
System.Reflection.Assembly.GetCallingAssembly().GetName().Name
)
{
}

/// <summary>
/// Deletes all temporary files from this location.
/// </summary>
/// <param name="suppressErrors">If true then errors deleting files are not thrown.</param>
public virtual void CleanTemporaryFiles
(
bool suppressErrors = true
)
{
// If we created the directory then remove it all.
if (this.CreatedDirectoryForThisUsage)
{
try
{
this.Directory.Delete(true);
Expand All @@ -108,9 +108,9 @@ public virtual void CleanTemporaryFiles
throw;
}
return;
}
}

// Otherwise, delete stuff inside the folder instead.
// Otherwise, delete stuff inside the folder instead.

// Remove all files.
foreach (var file in this.Directory.GetFiles())
Expand All @@ -132,7 +132,7 @@ public virtual void CleanTemporaryFiles
{
try
{
folder.Delete(true);
folder.Delete(true);
}
catch
{
Expand All @@ -142,74 +142,73 @@ public virtual void CleanTemporaryFiles
}
}
}

/// <summary>
/// Generates a unique (GUID-based) temporary file in <see cref="Directory"/> with the given <paramref name="extension"/>.
/// </summary>
/// <param name="extension">The extension for the file. If not provided defaults to ".tmp".</param>
/// <returns>A <see cref="FileInfo"/> for the temporary file.</returns>
protected FileInfo GenerateTemporaryFileInfo(string extension = null)
{
// Ensure the extension is valid.
if (string.IsNullOrWhiteSpace(extension))
extension = FileDownloadLocation.DefaultFileExtension;
if (false == extension.StartsWith("."))
extension = "." + extension;

// Create the file info.
return new FileInfo(Path.Combine(this.Directory.FullName, Guid.NewGuid() + extension));
}

/// <summary>
/// Generates a unique (GUID-based) temporary file in <see cref="Directory"/> with the given <paramref name="objectFile"/>.
/// </summary>
/// <param name="objectFile">The file that will be downloaded. Uses the <see cref="ObjectFile.Extension"/>.</param>
/// <returns>A <see cref="FileInfo"/> for the temporary file.</returns>
protected FileInfo GenerateTemporaryFileInfo(ObjectFile objectFile)
{
// Sanity.
if (null == objectFile)
throw new ArgumentNullException(nameof(objectFile));

// Use the other overload.
return this.GenerateTemporaryFileInfo(objectFile.Extension);
}

/// <summary>
/// Downloads the <paramref name="objectFile"/> from the <paramref name="vault"/>.
/// </summary>
/// <param name="objectFile">The file to download.</param>
/// <param name="vault">The vault to download from.</param>
/// <param name="blockSize">The size of blocks to use to transfer the file from the M-Files vault to this machine.</param>
/// <param name="fileFormat">The format of file to request from server.</param>
/// <returns>A <see cref="TemporaryFileDownload"/> representing the downloaded file.</returns>
public TemporaryFileDownload DownloadFile
(
ObjectFile objectFile,
Vault vault,
int blockSize = FileTransfers.DefaultBlockSize,
MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
)
{
return objectFile.Download
(
vault,
this.GenerateTemporaryFileInfo(objectFile),
blockSize,
fileFormat
);
}

/// <inheritdoc />
protected override void DisposeManagedObjects()
{
// If we should clean on disposal then clean.
if (this.CleanDirectoryOnDisposal)
this.CleanTemporaryFiles();

// Call the base implementation.
base.DisposeManagedObjects();
}

}

/// <summary>
/// Generates a unique (GUID-based) temporary file in <see cref="Directory"/> with the given <paramref name="extension"/>.
/// </summary>
/// <param name="extension">The extension for the file. If not provided defaults to ".tmp".</param>
/// <returns>A <see cref="FileInfo"/> for the temporary file.</returns>
protected FileInfo GenerateTemporaryFileInfo(string extension = null)
{
// Ensure the extension is valid.
if (string.IsNullOrWhiteSpace(extension))
extension = FileDownloadLocation.DefaultFileExtension;
if (false == extension.StartsWith("."))
extension = "." + extension;

// Create the file info.
return new FileInfo(Path.Combine(this.Directory.FullName, Guid.NewGuid() + extension));
}

/// <summary>
/// Generates a unique (GUID-based) temporary file in <see cref="Directory"/> with the given <paramref name="objectFile"/>.
/// </summary>
/// <param name="objectFile">The file that will be downloaded. Uses the <see cref="ObjectFile.Extension"/>.</param>
/// <returns>A <see cref="FileInfo"/> for the temporary file.</returns>
protected FileInfo GenerateTemporaryFileInfo(ObjectFile objectFile)
{
// Sanity.
if (null == objectFile)
throw new ArgumentNullException(nameof(objectFile));

// Use the other overload.
return this.GenerateTemporaryFileInfo(objectFile.Extension);
}

/// <summary>
/// Downloads the <paramref name="objectFile"/> from the <paramref name="vault"/>.
/// </summary>
/// <param name="objectFile">The file to download.</param>
/// <param name="vault">The vault to download from.</param>
/// <param name="blockSize">The size of blocks to use to transfer the file from the M-Files vault to this machine.</param>
/// <param name="fileFormat">The format of file to request from server.</param>
/// <returns>A <see cref="TemporaryFileDownload"/> representing the downloaded file.</returns>
public TemporaryFileDownload DownloadFile
(
ObjectFile objectFile,
Vault vault,
int blockSize = FileTransfers.DefaultBlockSize,
MFFileFormat fileFormat = MFFileFormat.MFFileFormatNative
)
{
return objectFile.Download
(
vault,
this.GenerateTemporaryFileInfo(objectFile),
blockSize,
fileFormat
);
}

/// <inheritdoc />
protected override void Dispose(bool disposing)
{
// If we should clean on disposal then clean.
if (this.CleanDirectoryOnDisposal)
this.CleanTemporaryFiles();

base.Dispose(disposing);
}

}
}
18 changes: 18 additions & 0 deletions MFilesAPI.Extensions/MFilesAPI.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@
<PackageIcon>logo.png</PackageIcon>
<PackageIconUrl />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net45|AnyCPU'">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net45|AnyCPU'">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard2.0|AnyCPU'">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net472|AnyCPU'">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net472|AnyCPU'">
<DebugType>full</DebugType>
</PropertyGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
Expand Down

0 comments on commit 65a53ed

Please sign in to comment.