1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
using HyperBooru.Services;
using ImageMagick;
using Microsoft.AspNetCore.Mvc;
using MimeDetective;
using System.Security.Cryptography;
namespace HyperBooru.Controllers;
[ApiController]
[Route("/media")]
public class MediaController : Controller {
private IConfigService config;
private HBContext db;
private ContentInspector inspector;
public MediaController(IConfigService config, HBContext db) {
this.config = config;
this.db = db;
ContentInspectorBuilder inspectorBuilder = new() {
Definitions =
MimeDetective.Definitions.Default.FileTypes.Images.All()
.Union(MimeDetective.Definitions.Default.FileTypes.Video.All())
.ToList()
};
inspector = inspectorBuilder.Build();
}
[HttpGet("list")]
public IActionResult EnumerateMedia() =>
Ok(db.Media.Select(m => m.ObjectId).ToArray());
[HttpGet("{mediaId}")]
public IActionResult Fetch([FromRoute] Guid mediaId) {
var media = db.Media.First(m => m.Guid == mediaId);
if(media is null)
return NotFound();
var fs = System.IO.File.OpenRead(config.GetPath(media));
return new FileStreamResult(fs, media.MimeType);
}
[HttpGet("thumb/{mediaId}")]
public IActionResult Thumbnail(
[FromRoute] Guid mediaId,
[FromQuery] int? w,
[FromQuery] int? h) {
var media = db.Media.First(m => m.Guid == mediaId);
if(media is null)
return NotFound();
if(media.MimeType.Split("/")[0] != "image")
return BadRequest("Media object not an image");
using var image = new MagickImage(config.GetPath(media));
if(w is null && h is null)
return BadRequest("Both width and height cannot be null!");
if(w > image.Width || h > image.Height)
return BadRequest("Requested thumbnail size is larger than original media");
int width = (int)(w is not null ? w : image.Width * h / image.Height);
int height = (int)(h is not null ? h : image.Height * w / image.Width);
var thumbPath = config.GetPath(media, width, height);
if(!System.IO.File.Exists(thumbPath)) {
image.Resize(new MagickGeometry(width, height));
image.Write(thumbPath);
}
var fs = System.IO.File.OpenRead(thumbPath);
return new FileStreamResult(fs, "image/jpeg");
}
[HttpDelete("{mediaId}")]
public IActionResult Delete([FromRoute] Guid mediaId) {
var media = db.Media.First(m => m.Guid == mediaId);
if(media is null)
return NotFound();
System.IO.File.Delete(config.GetPath(media));
db.Media.Remove(media);
db.SaveChanges();
return Ok();
}
[HttpPost]
public IActionResult Upload(
[FromForm] string? checksum,
[FromForm] DateTime? lastAccessTime,
[FromForm] DateTime? lastWriteTime,
[FromForm] DateTime? createTime) {
using var transaction = db.Database.BeginTransaction();
var formFile = Request.Form.Files[0];
if(formFile.Length < 1)
return BadRequest("Empty file");
var formStream = formFile.OpenReadStream();
// Calculate the checksum using the in-memory file contents
var hash = BitConverter
.ToString(MD5.Create().ComputeHash(formStream))
.Replace("-", "")
.ToLower();
if(checksum is not null && hash != checksum.ToLower())
return BadRequest("Checksum does not match");
var fileRecord = new UploadedFile() {
Filename = formFile.FileName,
OriginalChecksum = hash,
UploadTime = DateTime.UtcNow,
LastAccessTime = lastAccessTime,
LastWriteTime = lastWriteTime,
CreateTime = createTime
};
formStream.Seek(0, SeekOrigin.Begin);
var defs = inspector.Inspect(formStream);
var mime = defs.ByMimeType().FirstOrDefault()?.MimeType;
if(mime is null)
return BadRequest("Unsupported file type");
var media = db.Media
.FirstOrDefault(m => m.Checksum == hash);
if(media is null) {
var ingestTagDef = db.TagDefinitions
.First(td => td.Source == TagSource.Internal && td.Name == "ingest");
media = new() {
Checksum = hash,
MimeType = mime,
UploadedFiles = new() {
fileRecord
},
Tags = new() {
new() { TagDefinition = ingestTagDef }
}
};
using var newFile = System.IO.File.Create(config.GetPath(media));
formStream.Seek(0, SeekOrigin.Begin);
formStream.CopyTo(newFile);
newFile.Flush();
db.Media.Add(media);
} else {
media.UploadedFiles.Add(fileRecord);
db.Update(media);
}
db.SaveChanges();
transaction.Commit();
return Ok(media.Guid);
}
}
|