-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Downloading cache that will either fetch or load local or remote …
…files.
- Loading branch information
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.