summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/DB.cs3
-rw-r--r--src/Program.cs15
-rw-r--r--src/Util.cs14
3 files changed, 19 insertions, 13 deletions
diff --git a/src/DB.cs b/src/DB.cs
index cdd8479..595d3fe 100644
--- a/src/DB.cs
+++ b/src/DB.cs
@@ -11,6 +11,7 @@ public sealed class DB : DbContext
_created = true;
Database.EnsureDeleted();
Database.EnsureCreated();
+ Util.GetFilesDirectoryPath(true);
}
}
@@ -18,7 +19,7 @@ public sealed class DB : DbContext
public DbSet<Paste> Pastes { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
- optionsBuilder.UseSqlite("data source = main.db");
+ optionsBuilder.UseSqlite("data source = AppData/main.db");
base.OnConfiguring(optionsBuilder);
}
}
diff --git a/src/Program.cs b/src/Program.cs
index 89a8e02..abf516e 100644
--- a/src/Program.cs
+++ b/src/Program.cs
@@ -13,6 +13,7 @@ app.MapPost("/upload/{id}", UploadBig);
app.MapPost("/upload", UploadSimple);
app.MapPost("/text", UploadText);
app.MapGet("/b/{id}", GetBlob);
+Util.GetFilesDirectoryPath(true);
app.Run();
IResult GetUploadLink(HttpContext context, DB db) {
@@ -47,9 +48,8 @@ async Task<IResult> UploadSimple(HttpContext context, DB db) {
file.PasswordHash = PasswordHelper.HashPassword(context.Request.Form["password"]);
}
-
await using var write = System.IO.File.OpenWrite(
- Path.Combine(GetFilesDirectoryPath(), file.Id.ToString())
+ Path.Combine(Util.GetFilesDirectoryPath(), file.Id.ToString())
);
await context.Request.Form.Files[0].CopyToAsync(write);
db.Files.Add(file);
@@ -74,21 +74,12 @@ async Task<IResult> GetBlob(string id, DB db) {
if (file == default) return Results.NotFound();
var reader = await System.IO.File.ReadAllBytesAsync(
Path.Combine(
- GetFilesDirectoryPath(), file.Id.ToString()
+ Util.GetFilesDirectoryPath(), file.Id.ToString()
)
);
return Results.File(reader, file.MimeType, file.Name);
}
-string GetFilesDirectoryPath() {
- var filesDirectoryPath = Path.Combine(
- Directory.GetCurrentDirectory(),
- "AppData",
- "files"
- );
- Directory.CreateDirectory(filesDirectoryPath);
- return filesDirectoryPath;
-}
string GetUnusedBlobId(DB db) {
string id() => RandomString.Generate(3);
diff --git a/src/Util.cs b/src/Util.cs
new file mode 100644
index 0000000..19a433a
--- /dev/null
+++ b/src/Util.cs
@@ -0,0 +1,14 @@
+namespace BlobBin;
+
+public static class Util
+{
+ public static string GetFilesDirectoryPath(bool createIfNotExists = false) {
+ var filesDirectoryPath = Path.Combine(
+ Directory.GetCurrentDirectory(),
+ "AppData",
+ "files"
+ );
+ if (createIfNotExists) Directory.CreateDirectory(filesDirectoryPath);
+ return filesDirectoryPath;
+ }
+} \ No newline at end of file