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 all commits
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
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
38 changes: 32 additions & 6 deletions Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,37 @@ private static IDownloadCache Cache
_ => new DiskDownloadCache()
};

private static string downloadLocation = Application.temporaryCachePath;
private static List<string> allowedDownloadLocations = new()
{
Application.temporaryCachePath,
Application.persistentDataPath,
Application.dataPath,
Application.streamingAssetsPath
};

public static string DownloadLocation
{
get => downloadLocation;

set
{
if (allowedDownloadLocations.Contains(value))
{
downloadLocation = value;
}
else
{
Debug.LogError($"Invalid Download location specified");
}
}
}

/// <summary>
/// The download cache directory.<br/>
/// </summary>
public static string DownloadCacheDirectory
=> Path.Combine(Application.temporaryCachePath, download_cache);
=> Path.Combine(DownloadLocation, download_cache);

/// <summary>
/// Creates the <see cref="DownloadCacheDirectory"/> if it doesn't exist.
Expand All @@ -440,8 +466,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 @@ -505,7 +531,7 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName)
}
else
{
isCached = TryGetDownloadCacheItem(fileName, out cachePath);
isCached = TryGetDownloadCacheItem(fileName, out cachePath, parameters);
}

if (isCached)
Expand Down Expand Up @@ -583,7 +609,7 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName)
}
else
{
isCached = TryGetDownloadCacheItem(fileName, out cachePath);
isCached = TryGetDownloadCacheItem(fileName, out cachePath, parameters);
}

if (isCached)
Expand Down Expand Up @@ -898,7 +924,7 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName)
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,6 +21,7 @@ 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>
/// <param name="debug">Optional,Enabled printing for the debug output of the request.</param>
public RestParameters(
IReadOnlyDictionary<string, string> headers = null,
Expand All @@ -30,6 +31,7 @@ public class RestParameters
bool disposeUploadHandler = true,
CertificateHandler certificateHandler = null,
bool disposeCertificateHandler = true,
bool forceDownload = false,
bool debug = false)
{
Headers = headers;
Expand All @@ -39,6 +41,7 @@ public class RestParameters
DisposeUploadHandler = disposeUploadHandler;
CertificateHandler = certificateHandler;
DisposeCertificateHandler = disposeCertificateHandler;
ForceDownload = forceDownload;
Debug = debug;
}

Expand Down Expand Up @@ -82,6 +85,11 @@ public class RestParameters

internal int ServerSentEventCount { get; set; }

/// <summary>
/// Force the download cache to invalidate the existing file and download the file fresh again<br/>Default is false.
/// </summary>
public bool ForceDownload { get; internal set; }

/// <summary>
/// Enabled printing for the debug output of the request.
/// </summary>
Expand Down