Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Commit

Permalink
Rename/Move LocalStorageService
Browse files Browse the repository at this point in the history
  • Loading branch information
j2ghz committed Jul 3, 2018
1 parent ce001bf commit 34bf82d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

namespace Modsink.Common.Tests.Client
{
public class LocalStorageServiceTests
public class FileAccessServiceTests
{
[Fact]
public async Task CheckLocationTest()
{
var lss = new LocalStorageService(new DirectoryInfo(Path.Combine(Path.GetFullPath("."), "temp\\")));
var lss = new FileAccessService(new DirectoryInfo(Path.Combine(Path.GetFullPath("."), "temp\\")));
var fileSignature =
new FileSignature(new HashValue(new byte[] {0x99, 0xE9, 0xD8, 0x51, 0x37, 0xDB, 0x46, 0xEF}), 1UL);

Expand Down
8 changes: 4 additions & 4 deletions src/ModSink.Common/Client/ClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ namespace ModSink.Common.Client
{
public class ClientService : ReactiveObject
{
public ClientService(DownloadService downloadService, ILocalStorageService localStorageService,
public ClientService(DownloadService downloadService, IFileAccessService fileAccessService,
IDownloader downloader, IFormatter serializationFormatter)
{
DownloadService = downloadService;
LocalStorageService = localStorageService;
FileAccessService = fileAccessService;
Downloader = downloader;
SerializationFormatter = serializationFormatter;

Expand All @@ -42,7 +42,7 @@ public ClientService(DownloadService downloadService, ILocalStorageService local
public IObservableList<Group> Groups { get; }
public ISourceList<string> GroupUrls { get; } = new SourceList<string>();
public DownloadService DownloadService { get; }
public ILocalStorageService LocalStorageService { get; }
public IFileAccessService FileAccessService { get; }
public IObservableList<Repo> Repos { get; }


Expand All @@ -53,7 +53,7 @@ public async Task ScheduleMissingFilesDownload(Modpack modpack)
foreach (var fh in mod.Mod.Files)
{
var fileSignature = fh.Value;
var (available, stream) = await LocalStorageService.WriteIfMissingOrInvalid(fileSignature);
var (available, stream) = await FileAccessService.WriteIfMissingOrInvalid(fileSignature);
LogTo.Debug("Check {fh}, Exists: {exists}", fileSignature.Hash, available);
if (!available)
DownloadService.Add(new Download(GetDownloadUri(fileSignature), stream,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DynamicData;
using ModSink.Common.Models.Repo;

namespace ModSink.Common.Client
{
public class LocalStorageService : ILocalStorageService
public class FileAccessService : IFileAccessService
{
private readonly SourceList<FileSignature> filesAvailable = new SourceList<FileSignature>();
private readonly DirectoryInfo localDir;

public LocalStorageService(DirectoryInfo localDir)
public FileAccessService(DirectoryInfo localDir)
{
this.localDir = localDir;
if (!localDir.Exists)
localDir.Create();
filesAvailable.Edit(l =>
{
l.AddRange(localDir.EnumerateFiles()
.Select(fi => new FileSignature(new HashValue(fi.Name), fi.Length)));
});
}

public IObservableList<FileSignature> FilesAvailable => filesAvailable.AsObservableList();

public async Task Delete(FileSignature fileSignature)
{
var fi = await GetFileInfo(fileSignature);
filesAvailable.Remove(fileSignature);
await Task.Run(() => fi.Delete());
}

Expand All @@ -47,7 +37,7 @@ public FileInfo GetFileUri(FileSignature fileSignature)
{
return localDir.ChildFile(GetFileName(fileSignature));
}
[Obsolete]

public async Task<bool> IsFileAvailable(FileSignature fileSignature)
{
var fi = await GetFileInfo(fileSignature);
Expand All @@ -57,13 +47,13 @@ public async Task<bool> IsFileAvailable(FileSignature fileSignature)
public async Task<Stream> Read(FileSignature fileSignature)
{
var file = await GetFileInfo(fileSignature);
return await Task.Run(() => file.Open(FileMode.Open, FileAccess.Read,FileShare.Read));
return await Task.Run(() => file.Open(FileMode.Open, FileAccess.Read, FileShare.Read));
}

public async Task<Stream> Write(FileSignature fileSignature)
{
var file = await GetFileInfo(fileSignature);
return await Task.Run(() => file.Open(FileMode.Create, FileAccess.Write,FileShare.None));
return await Task.Run(() => file.Open(FileMode.Create, FileAccess.Write, FileShare.None));
}

public async Task<(bool available, Lazy<Task<Stream>> stream)> WriteIfMissingOrInvalid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace ModSink.Common.Client
{
public interface ILocalStorageService
public interface IFileAccessService
{
Task Delete(FileSignature fileSignature);

Expand Down
2 changes: 1 addition & 1 deletion src/ModSink.Common/ModSinkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ModSinkBuilder InDirectory(DirectoryInfo directory)
public ModSink Build()
{
return new ModSink(new ClientService(new DownloadService(downloader),
new LocalStorageService(localStorageDirectory), downloader, formatter));
new FileAccessService(localStorageDirectory), downloader, formatter));
}
}
}

0 comments on commit 34bf82d

Please sign in to comment.