Skip to content

Commit

Permalink
WIP Downloading cache that will either fetch or load local or remote …
Browse files Browse the repository at this point in the history
…files.
  • Loading branch information
TimAidley committed Feb 9, 2023
1 parent f5a1f68 commit df9343b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Assets/Editor/Tests/TestDownloadingCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.IO;
using System.Linq;
using Assets.Oculus.VR.Editor;
using NUnit.Framework;
namespace TiltBrush
{
[TestFixture]
public class TestDownloadingCache
{
private DownloadingCache m_dlCache;
private FileCache m_Cache;
private string m_Path;

private const string kLocalFile = "file://TestData/main_1.png";
private const string kRemoteFile = "http://openbrush.app/assets/icon.png";

[SetUp]
public void Setup()
{
m_Path = Path.Combine(Path.GetTempPath(), "FileCacheTest");
if (File.Exists(m_Path))
{
File.Delete(m_Path);
}
if (Directory.Exists(m_Path))
{
Directory.Delete(m_Path, recursive: true);
}
m_Cache = new FileCache(m_Path, 1);
m_dlCache = new DownloadingCache(m_Cache);
}

[TearDown]
public void Teardown()
{
if (Directory.Exists(m_Path))
{
Directory.Delete(m_Path, recursive: true);
}
}

[Test]
public async void LocalFileLoads()
{
var bytes = await m_dlCache.Read("test", "logo1", kLocalFile);
Assert.That(bytes != null);
Assert.That(bytes.Length == 32983);
}

[Test]
public async void RemoteFileLoads()
{
var bytes = await m_dlCache.Read("test", "logo1", kRemoteFile);
Assert.That(bytes != null);
Assert.That(bytes.Length == 32983);
}

[Test]
public async void RemoteFileIsStoredInCache()
{
var bytes = await m_dlCache.Read("test", "logo1", kRemoteFile);
Assert.That(m_Cache.CacheSize == 32983);
}

[Test]
public async void RemoteFileCanBeLoadedFromCache()
{
var bytes = await m_dlCache.Read("test", "logo1", kRemoteFile);
var bytes2 = m_Cache.Read("test", "logo1");
Assert.That(Enumerable.SequenceEqual(bytes, bytes2));
var bytes3 = await m_dlCache.Read("test", "logo1", kRemoteFile);
Assert.That(Enumerable.SequenceEqual(bytes, bytes3));
}

}
}
3 changes: 3 additions & 0 deletions Assets/Editor/Tests/TestDownloadingCache.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/Scripts/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEngine;
Expand Down Expand Up @@ -75,6 +76,8 @@ public partial class App : MonoBehaviour
"All your " + kAppDisplayName + " files have been moved to\n" +
"/sdcard/" + kAppFolderName + ".\n";

public static HttpClient HttpClient = new HttpClient();

public enum AppState
{
Error,
Expand Down
56 changes: 56 additions & 0 deletions Assets/Scripts/Sharing/DownloadingCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace TiltBrush
{
/// <summary>
/// Cache where remote or local files can be requested, and remote files will be cached.
/// Local files will just be read directly.
/// </summary>
public class DownloadingCache
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="cache">File cache</param>
public DownloadingCache(FileCache cache)
{
m_Cache = cache;
}

// TODO: I think this needs to store metadata about where the file came from.
// and do something about files that have changed etc

/// <summary>
/// Read a file from a location. If the file does not exist, it will be cached
/// at the given fileset and filename.
/// </summary>
/// <param name="fileset">Fileset to store/retrieve</param>
/// <param name="filename">Filename</param>
/// <param name="url">Location to load - should be http(s):// or file://</param>
/// <returns>Task that returns the bytes for a file.</returns>
public async Task<byte[]> Read(string fileset, string filename, string url)
{
const string fileStart = "file://";
const string httpStart = "http";
if (m_Cache.FileExists(fileset, filename))
{
return m_Cache.Read(fileset, filename);
}
if (url.StartsWith(fileStart))
{
return File.ReadAllBytes(url.Skip(fileStart.Length).ToString());
}
else if (url.StartsWith(httpStart))
{
byte[] bytes = await App.HttpClient.GetByteArrayAsync(url);
m_Cache.Write(fileset, filename, bytes);
return bytes;
}
return null;
}

private FileCache m_Cache;
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Sharing/DownloadingCache.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit df9343b

Please sign in to comment.