Skip to content

Commit 47576fc

Browse files
authored
Added support for subcategories specified via subfolders
1 parent be62fd3 commit 47576fc

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

OpenBulletAPI/Services/ConfigService.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,33 @@ public MemoryStream Get(string[] groups)
3030
foreach (var group in groups.Where(g => g.Trim() != ""))
3131
{
3232
var dir = Path.Combine(_configFolder, group);
33-
if (Directory.Exists(dir))
34-
{
35-
foreach(var file in Directory.EnumerateFiles(dir).Where(file => file.EndsWith(".loli")))
36-
{
37-
var zipArchiveEntry = archive.CreateEntry(Path.GetFileName(file), CompressionLevel.Fastest);
38-
var fileContent = File.ReadAllBytes(file);
39-
using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(fileContent, 0, fileContent.Length);
40-
}
41-
}
33+
34+
ZipFolderRecursively(archive, dir, _configFolder);
4235
}
4336
}
4437

4538
return ms;
4639
}
4740
}
4841

42+
private void ZipFolderRecursively(ZipArchive archive, string currentDir, string baseDir)
43+
{
44+
// Add all the files
45+
foreach (var file in Directory.EnumerateFiles(currentDir).Where(file => file.EndsWith(".loli")))
46+
{
47+
// Create the file entry and write the file content
48+
var zipArchiveEntry = archive.CreateEntry($"{file.Substring(baseDir.Length + 1)}", CompressionLevel.Fastest);
49+
var fileContent = File.ReadAllBytes(file);
50+
using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(fileContent, 0, fileContent.Length);
51+
}
52+
53+
// Add subfolders recursively
54+
foreach (var dir in Directory.EnumerateDirectories(currentDir))
55+
{
56+
ZipFolderRecursively(archive, dir, baseDir);
57+
}
58+
}
59+
4960
public async Task<bool> Upload(Stream file, string group, string name)
5061
{
5162
if (group != "" && name.EndsWith(".loli"))

0 commit comments

Comments
 (0)