Skip to content
Merged
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
82 changes: 82 additions & 0 deletions Albums/ArchiveAlbum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.IO.Compression;
using CustomAlbums.Data;
using CustomAlbums.Utilities;

namespace CustomAlbums.Albums;

public class ArchiveAlbum : Album
{
public ArchiveAlbum(string path, int index)
{
if (!File.Exists(path))
{
Logger.Error($"Could not find album package at: {path}");
throw new FileNotFoundException();
}

using var zip = ZipFile.OpenRead(path);
var info = zip.GetEntry("info.json");
if (info == null)
{
Logger.Error($"Could not find info.json in package: {path}");
throw new FileNotFoundException();
}

using var stream = info.Open();
Info = Json.Deserialize<AlbumInfo>(stream);
HasPng = zip.GetEntry("cover.png") != null;

Index = index;
Path = path;

GetSheets();
}

public override string AlbumName => $"album_{System.IO.Path.GetFileNameWithoutExtension(Path)}";

public override bool HasFile(string name)
{
if (!File.Exists(Path)) return false;
try
{
using var zip = ZipFile.OpenRead(Path);
return zip.GetEntry(name) != null;
}
catch (IOException)
{
return false;
}
}

public override Stream OpenFileStreamIfPossible(string file)
{
using var zip = ZipFile.OpenRead(Path);
var entry = zip.GetEntry(file);

if (entry != null) return entry.Open().ToMemoryStream();

Logger.Error($"Could not find file in package: {file}");
throw new FileNotFoundException();
}

public override Stream OpenNullableStream(string file)
{
using var zip = ZipFile.OpenRead(Path);
var entry = zip.GetEntry(file);

if (entry != null) return entry.Open().ToMemoryStream();

return null;
}

public override MemoryStream OpenMemoryStream(string file)
{
using var zip = ZipFile.OpenRead(Path);
var entry = zip.GetEntry(file);

if (entry != null) return entry.Open().ToMemoryStream();

Logger.Error($"Could not find file in package: {file}");
throw new FileNotFoundException();
}
}
74 changes: 74 additions & 0 deletions Albums/FolderAlbum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using CustomAlbums.Data;
using CustomAlbums.Utilities;

namespace CustomAlbums.Albums;

public class FolderAlbum : Album
{
public FolderAlbum(string path, int index)
{
if (!Directory.Exists(path))
{
Logger.Error($"Could not find album directory at: {path}");
throw new DirectoryNotFoundException();
}

var infoPath = System.IO.Path.Combine(path, "info.json");
if (!File.Exists(infoPath))
{
Logger.Error($"Could not find info.json at: {infoPath}");
throw new FileNotFoundException();
}

using var fileStream = File.OpenRead(infoPath);
Info = Json.Deserialize<AlbumInfo>(fileStream);
HasPng = File.Exists(System.IO.Path.Combine(path, "cover.png"));

Index = index;
Path = path;

GetSheets();
}

public override string AlbumName => $"album_{System.IO.Path.GetFileName(Path)}_folder";

protected override string GetCoverUrl(string cacheDir)
{
return "file:///" + System.IO.Path.Combine(Path, "cover.png").Replace('\\', '/');
}

public override bool HasFile(string name)
{
var path = System.IO.Path.Combine(Path, name);
return File.Exists(path);
}

public override Stream OpenFileStreamIfPossible(string file)
{
var path = System.IO.Path.Combine(Path, file);
if (File.Exists(path))
return File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

Logger.Error($"Could not find file: {path}");
throw new FileNotFoundException();
}

public override Stream OpenNullableStream(string file)
{
var path = System.IO.Path.Combine(Path, file);
if (File.Exists(path))
return File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

return null;
}

public override MemoryStream OpenMemoryStream(string file)
{
var path = System.IO.Path.Combine(Path, file);
if (File.Exists(path))
return File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite).ToMemoryStream();

Logger.Error($"Could not find file: {path}");
throw new FileNotFoundException();
}
}
89 changes: 89 additions & 0 deletions Albums/PackAlbum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.IO.Compression;
using CustomAlbums.Data;
using CustomAlbums.Utilities;

namespace CustomAlbums.Albums;

public class PackAlbum : Album
{
public PackAlbum(string directory, ZipArchiveEntry mdm, int index, string packName = null)
{
if (!string.IsNullOrEmpty(packName)) PackName = packName;

using var mdmStream = mdm.Open();
using var openedZip = new ZipArchive(mdmStream);

var info = openedZip.GetEntry("info.json");
if (info == null)
{
Logger.Error($"Could not find info.json in package: {mdm.Name}");
throw new FileNotFoundException();
}

using var stream = info.Open();
Info = Json.Deserialize<AlbumInfo>(stream);
HasPng = openedZip.GetEntry("cover.png") != null;

Index = index;
Path = directory;
PackAlbumName = System.IO.Path.GetFileNameWithoutExtension(mdm.Name);

GetSheets();
}

public string PackName { get; }
public string PackAlbumName { get; }

public override string AlbumName =>
$"album_{(string.IsNullOrEmpty(PackAlbumName) ? System.IO.Path.GetFileNameWithoutExtension(Path) : string.Empty)}{(PackName != null ? $"{PackAlbumName}_{PackName}" : string.Empty)}";

public override bool HasFile(string name)
{
if (!File.Exists(Path)) return false;
try
{
using var mdp = ZipFile.OpenRead(Path);
using var openedMdm = mdp.GetNestedZip(PackAlbumName + ".mdm");
return openedMdm.GetEntry(name) != null;
}
catch (IOException)
{
return false;
}
}

public override Stream OpenFileStreamIfPossible(string file)
{
using var mdp = ZipFile.OpenRead(Path);
using var openedMdm = mdp.GetNestedZip(PackAlbumName + ".mdm");
var entry = openedMdm.GetEntry(file);

if (entry != null) return entry.Open().ToMemoryStream();

Logger.Error($"Could not find file in package: {file}");
throw new FileNotFoundException();
}

public override Stream OpenNullableStream(string file)
{
using var mdp = ZipFile.OpenRead(Path);
using var openedMdm = mdp.GetNestedZip(PackAlbumName + ".mdm");
var entry = openedMdm.GetEntry(file);

if (entry != null) return entry.Open().ToMemoryStream();

return null;
}

public override MemoryStream OpenMemoryStream(string file)
{
using var mdp = ZipFile.OpenRead(Path);
using var openedMdm = mdp.GetNestedZip(PackAlbumName + ".mdm");
var entry = openedMdm.GetEntry(file);

if (entry != null) return entry.Open().ToMemoryStream();

Logger.Error($"Could not find file in package: {file}");
throw new FileNotFoundException();
}
}
Loading
Loading