Compare commits

...

5 Commits

6 changed files with 56 additions and 21 deletions

View File

@ -68,7 +68,9 @@ impl MediaMetadata {
impl Database {
pub async fn get_missing_media_metadata(&mut self) -> Result<Vec<FileUpload>> {
let results: Vec<FileUpload> = sqlx::query_as("select * from uploads where (width is null or height is null or bitrate is null or duration is null) and (mime_type like 'image/%' or mime_type like 'video/%')")
let results: Vec<FileUpload> = sqlx::query_as("select * from uploads where \
(mime_type like 'image/%' and (width is null or height is null)) or \
(mime_type like 'video/%' and (width is null or height is null or bitrate is null or duration is null))")
.fetch_all(&self.pool)
.await?;

View File

@ -73,7 +73,13 @@ async fn main() -> Result<(), Error> {
.attach(Shield::new()) // disable
.mount(
"/",
routes![root, get_blob, head_blob, routes::void_cat_redirect, routes::void_cat_redirect_head],
routes![
root,
get_blob,
head_blob,
routes::void_cat_redirect,
routes::void_cat_redirect_head
],
)
.mount("/admin", routes::admin_routes());

View File

@ -25,7 +25,7 @@ pub struct BlobDescriptor {
pub size: u64,
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
pub created: u64,
pub uploaded: u64,
#[serde(rename = "nip94", skip_serializing_if = "Option::is_none")]
pub nip94: Option<HashMap<String, String>>,
}
@ -45,7 +45,7 @@ impl BlobDescriptor {
sha256: id_hex,
size: value.size,
mime_type: Some(value.mime_type.clone()),
created: value.created.timestamp() as u64,
uploaded: value.created.timestamp() as u64,
nip94: Some(
Nip94Event::from_upload(settings, value)
.tags

View File

@ -59,22 +59,27 @@ struct PagedResult<T> {
impl Nip94Event {
pub fn from_upload(settings: &Settings, upload: &FileUpload) -> Self {
let hex_id = hex::encode(&upload.id);
let ext = if upload.mime_type != "application/octet-stream" {
mime2ext::mime2ext(&upload.mime_type)
} else {
None
};
let mut tags = vec![
vec![
"url".to_string(),
format!(
"{}/{}{}",
&settings.public_url,
&hex_id,
mime2ext::mime2ext(&upload.mime_type)
.map(|m| format!(".{m}"))
.unwrap_or("".to_string())
),
format!("{}/{}.{}", &settings.public_url, &hex_id, ext.unwrap_or("")),
],
vec!["x".to_string(), hex_id],
vec!["x".to_string(), hex_id.clone()],
vec!["m".to_string(), upload.mime_type.clone()],
vec!["size".to_string(), upload.size.to_string()],
];
if upload.mime_type.starts_with("image/") || upload.mime_type.starts_with("video/") {
tags.push(vec![
"thumb".to_string(),
format!("{}/thumb/{}.webp", &settings.public_url, &hex_id),
]);
}
if let Some(bh) = &upload.blur_hash {
tags.push(vec!["blurhash".to_string(), bh.clone()]);
}
@ -402,6 +407,10 @@ pub async fn get_blob_thumb(
return Err(Status::NotFound);
};
if !(info.mime_type.starts_with("image/") || info.mime_type.starts_with("video/")) {
return Err(Status::NotFound);
}
let file_path = fs.get(&id);
let mut thumb_file = temp_dir().join(format!("thumb_{}", sha256));
@ -437,10 +446,15 @@ pub async fn void_cat_redirect(id: &str, settings: &State<Settings>) -> Option<N
id
};
if let Some(base) = &settings.void_cat_files {
let uuid =
uuid::Uuid::from_slice_le(nostr::bitcoin::base58::decode(id).unwrap().as_slice())
.unwrap();
let f = base.join(VoidFile::map_to_path(&uuid));
let uuid = if let Ok(b58) = nostr::bitcoin::base58::decode(id) {
uuid::Uuid::from_slice_le(b58.as_slice())
} else {
uuid::Uuid::parse_str(id)
};
if uuid.is_err() {
return None;
}
let f = base.join(VoidFile::map_to_path(&uuid.unwrap()));
debug!("Legacy file map: {} => {}", id, f.display());
if let Ok(f) = NamedFile::open(f).await {
Some(f)

View File

@ -22,9 +22,9 @@ export class Route96 {
return data;
}
async listFiles(page = 0, count = 10) {
async listFiles(page = 0, count = 10, mime: string | undefined) {
const rsp = await this.#req(
`admin/files?page=${page}&count=${count}`,
`admin/files?page=${page}&count=${count}${mime ? `&mime_type=${mime}` : ""}`,
"GET",
);
const data = await this.#handleResponse<AdminResponseFileList>(rsp);

View File

@ -23,6 +23,7 @@ export default function Upload() {
const [adminListedFiles, setAdminListedFiles] = useState<Nip96FileList>();
const [listedPage, setListedPage] = useState(0);
const [adminListedPage, setAdminListedPage] = useState(0);
const [mimeFilter, setMimeFilter] = useState<string>();
const login = useLogin();
const pub = usePublisher();
@ -85,7 +86,7 @@ export default function Upload() {
try {
setError(undefined);
const uploader = new Route96(url, pub);
const result = await uploader.listFiles(n, 50);
const result = await uploader.listFiles(n, 50, mimeFilter);
setAdminListedFiles(result);
} catch (e) {
if (e instanceof Error) {
@ -135,7 +136,7 @@ export default function Upload() {
useEffect(() => {
listAllUploads(adminListedPage);
}, [adminListedPage]);
}, [adminListedPage, mimeFilter]);
useEffect(() => {
if (pub && !self) {
@ -249,6 +250,18 @@ export default function Upload() {
<hr />
<h3>Admin File List:</h3>
<Button onClick={() => listAllUploads(0)}>List All Uploads</Button>
<div>
<select value={mimeFilter} onChange={e => setMimeFilter(e.target.value)}>
<option value={""}>All</option>
<option>image/webp</option>
<option>image/jpeg</option>
<option>image/jpg</option>
<option>image/png</option>
<option>image/gif</option>
<option>video/mp4</option>
<option>video/mov</option>
</select>
</div>
{adminListedFiles && (
<FileList
files={adminListedFiles.files}