upload images (untested)

This commit is contained in:
Brandon Thomas committed 2023-07-30 18:13:46 -04:00
1 parent e87091c5cb
commit a52401176e
3 files changed
+61 -2

No files matched your search

@@ -186,6 +186,10 @@ pub async fn handle_upload(
"audio/opus" /* .opus */ => Some(MediaUploadType::Audio),
"audio/x-flac" /* .flac */ => Some(MediaUploadType::Audio),
"audio/x-wav" /* .wav */ => Some(MediaUploadType::Audio),
// Image
"image/jpeg" /* .jpg */ => Some(MediaUploadType::Image),
"image/png" /* .png */ => Some(MediaUploadType::Image),
"image/webp" /* .webp */ => Some(MediaUploadType::Image),
// Video
"video/mp4" /* .mp4 */ => Some(MediaUploadType::Video),
"video/quicktime" /* .mov */ => Some(MediaUploadType::Video),
@@ -193,7 +197,7 @@ pub async fn handle_upload(
_ => None,
};
let do_decode = match mimetype {
let do_audio_decode = match mimetype {
"audio/opus" => {
// TODO/FIXME(bt, 2023-05-19): Symphonia is currently broken for Firefox's opus.
// We're on an off-master branch that may resolve the problem in the future, but for now
@@ -219,10 +223,14 @@ pub async fn handle_upload(
// right: `Ebml`: EBML element type must be checked before calling this function', /Users/bt/.cargo/git/checkouts/symphonia-8fbe6c90fc095688/e1a7009/symphonia-format-mkv/src/ebml.rs:335:9
false
}
// Also, don't decode images
"image/jpeg" => false,
"image/png" => false,
"image/webp" => false,
_ => true,
};
if do_decode && media_upload_type.is_some() {
if do_audio_decode && media_upload_type.is_some() {
let basic_info = decode_basic_audio_bytes_info(
bytes.as_ref(),
Some(mimetype),
@@ -1,4 +1,5 @@
pub mod common;
pub mod list_user_media_uploads_of_type;
pub mod upload_audio;
pub mod upload_image;
pub mod upload_media;
@@ -0,0 +1,50 @@
use actix_multipart::Multipart;
use actix_web::{HttpRequest, HttpResponse, web};
use crate::http_server::endpoints::media_uploads::common::handle_upload::handle_upload;
use crate::http_server::endpoints::media_uploads::common::upload_error::UploadError;
use crate::server_state::ServerState;
use once_cell::sync::Lazy;
use std::collections::HashSet;
use std::sync::Arc;
use tokens::files::media_upload::MediaUploadToken;
#[derive(Serialize)]
pub struct UploadAudioSuccessResponse {
pub success: bool,
pub upload_token: MediaUploadToken,
}
static ALLOWED_MIME_TYPES : Lazy<HashSet<&'static str>> = Lazy::new(|| {
HashSet::from([
"image/jpeg",
"image/png",
"image/webp",
])
});
pub async fn upload_image_handler(
http_request: HttpRequest,
server_state: web::Data<Arc<ServerState>>,
mut multipart_payload: Multipart,
) -> Result<HttpResponse, UploadError> {
let response = handle_upload(
&http_request,
&server_state,
multipart_payload,
&ALLOWED_MIME_TYPES).await?;
let media_upload_token = response.to_media_token();
let response = UploadAudioSuccessResponse {
success: true,
upload_token: media_upload_token,
};
let body = serde_json::to_string(&response)
.map_err(|e| UploadError::ServerError)?;
return Ok(HttpResponse::Ok()
.content_type("application/json")
.body(body));
}