Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ Additional Features #65

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added feature to enable "forcedownloading" an item, thus overwriting/…
…replacing the cache.
  • Loading branch information
SimonDarksideJ committed Jan 6, 2024
commit 3482b3d7a81c1a84923c61032428209f9902810b
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task ValidateCacheDirectoryAsync()
}

/// <inheritdoc />
public bool TryGetDownloadCacheItem(string uri, out string filePath)
public bool TryGetDownloadCacheItem(string uri, out string filePath, RestParameters restParameters)
{
ValidateCacheDirectory();
bool exists;
Expand All @@ -66,6 +66,12 @@ public bool TryGetDownloadCacheItem(string uri, out string filePath)

if (exists)
{
if (restParameters != null && restParameters.ForceDownload)
{
Debug.Log($"Force download of {uri}/{filePath}");
return !TryDeleteCacheItem(uri);
}

filePath = $"{Rest.FileUriPrefix}{Path.GetFullPath(filePath)}";
}

Expand All @@ -75,21 +81,26 @@ public bool TryGetDownloadCacheItem(string uri, out string filePath)
/// <inheritdoc />
public bool TryDeleteCacheItem(string uri)
{
if (!TryGetDownloadCacheItem(uri, out var filePath))
if (!TryGetDownloadCacheItem(uri, out var filePath, null))
{
return false;
}

var absoluteFilePath = filePath.Replace(Rest.FileUriPrefix, string.Empty);

try
{
File.Delete(filePath);
if (File.Exists(absoluteFilePath))
{
File.Delete(absoluteFilePath);
}
}
catch (Exception e)
{
Debug.LogError(e);
}

return !File.Exists(filePath);
return !File.Exists(absoluteFilePath);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal interface IDownloadCache

Task ValidateCacheDirectoryAsync();

bool TryGetDownloadCacheItem(string uri, out string filePath);
bool TryGetDownloadCacheItem(string uri, out string filePath, RestParameters restParameters);

bool TryDeleteCacheItem(string uri);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
using System.Threading.Tasks;
using Utilities.WebRequestRest.Interfaces;

namespace Utilities.Rest
namespace Utilities.WebRequestRest
{
internal class NoOpDownloadCache : IDownloadCache
{
public void ValidateCacheDirectory() { }

public Task ValidateCacheDirectoryAsync() => Task.CompletedTask;

public bool TryGetDownloadCacheItem(string uri, out string filePath)
public bool TryGetDownloadCacheItem(string uri, out string filePath, RestParameters restParameters)
{
filePath = uri;
return false;
Expand Down
10 changes: 5 additions & 5 deletions Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ public static Task ValidateCacheDirectoryAsync()
/// <param name="uri">The uri key of the item.</param>
/// <param name="filePath">The file path to the cached item.</param>
/// <returns>True, if the item was in cache, otherwise false.</returns>
public static bool TryGetDownloadCacheItem(string uri, out string filePath)
=> Cache.TryGetDownloadCacheItem(uri, out filePath);
public static bool TryGetDownloadCacheItem(string uri, out string filePath, RestParameters restParameters)
=> Cache.TryGetDownloadCacheItem(uri, out filePath, restParameters);

/// <summary>
/// Try to delete the cached item at the uri.
Expand Down Expand Up @@ -541,7 +541,7 @@ public static async Task<Texture2D> DownloadTextureAsync(
}
else
{
isCached = TryGetDownloadCacheItem(fileName, out cachePath);
isCached = TryGetDownloadCacheItem(fileName, out cachePath, parameters);
}

if (isCached)
Expand Down Expand Up @@ -631,7 +631,7 @@ public static async Task<AudioClip> DownloadAudioClipAsync(
}
else
{
isCached = TryGetDownloadCacheItem(fileName, out cachePath);
isCached = TryGetDownloadCacheItem(fileName, out cachePath, parameters);
}

if (isCached)
Expand Down Expand Up @@ -969,7 +969,7 @@ public static async Task<string> DownloadFileAsync(
TryGetFileNameFromUrl(url, out fileName);
}

if (TryGetDownloadCacheItem(fileName, out var filePath))
if (TryGetDownloadCacheItem(fileName, out var filePath, parameters))
{
return filePath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ public class RestParameters
/// <param name="disposeUploadHandler">Optional, dispose the <see cref="UploadHandler"/>?<br/>Default is true.</param>
/// <param name="certificateHandler">Optional, certificate handler for the request.</param>
/// <param name="disposeCertificateHandler">Optional, dispose the <see cref="CertificateHandler"/>?<br/>Default is true.</param>
/// <param name="forceDownload">Optional, Force the download cache to invalidate the existing file and download the file fresh again<br/>Default is false.</param>
public RestParameters(
IReadOnlyDictionary<string, string> headers = null,
IProgress<Progress> progress = null,
int timeout = -1,
bool disposeDownloadHandler = true,
bool disposeUploadHandler = true,
CertificateHandler certificateHandler = null,
bool disposeCertificateHandler = true)
bool disposeCertificateHandler = true,
bool forceDownload = false)
{
Headers = headers;
Progress = progress;
Expand All @@ -37,6 +39,7 @@ public RestParameters(
DisposeUploadHandler = disposeUploadHandler;
CertificateHandler = certificateHandler;
DisposeCertificateHandler = disposeCertificateHandler;
ForceDownload = forceDownload;
}

/// <summary>
Expand Down Expand Up @@ -77,6 +80,8 @@ public RestParameters(
/// </summary>
public bool DisposeUploadHandler { get; internal set; }

public bool ForceDownload { get; internal set; }

internal int ServerSentEventCount { get; set; }
}
}