Can download ISO file by URL

This commit is contained in:
Pierre HUBERT 2023-09-05 16:12:20 +02:00
parent 4b55e17ee0
commit e7d5747b99
11 changed files with 231 additions and 9 deletions

View File

@ -863,6 +863,12 @@ version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c"
[[package]]
name = "futures-io"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964"
[[package]] [[package]]
name = "futures-macro" name = "futures-macro"
version = "0.3.28" version = "0.3.28"
@ -893,8 +899,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-io",
"futures-macro", "futures-macro",
"futures-sink",
"futures-task", "futures-task",
"memchr",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab", "slab",
@ -1528,10 +1537,12 @@ dependencies = [
"serde_urlencoded", "serde_urlencoded",
"tokio", "tokio",
"tokio-native-tls", "tokio-native-tls",
"tokio-util",
"tower-service", "tower-service",
"url", "url",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"wasm-streams",
"web-sys", "web-sys",
"winreg", "winreg",
] ]
@ -2000,9 +2011,11 @@ dependencies = [
"lazy_static", "lazy_static",
"light-openid", "light-openid",
"log", "log",
"reqwest",
"serde", "serde",
"serde_json", "serde_json",
"tempfile", "tempfile",
"url",
] ]
[[package]] [[package]]
@ -2086,6 +2099,19 @@ version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
[[package]]
name = "wasm-streams"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4609d447824375f43e1ffbc051b50ad8f4b3ae8219680c94452ea05eb240ac7"
dependencies = [
"futures-util",
"js-sys",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
]
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.64" version = "0.3.64"

View File

@ -9,7 +9,7 @@ edition = "2021"
log = "0.4.19" log = "0.4.19"
env_logger = "0.10.0" env_logger = "0.10.0"
clap = { version = "4.3.19", features = ["derive", "env"] } clap = { version = "4.3.19", features = ["derive", "env"] }
light-openid = { version = "1.0.1", features=["crypto-wrapper"] } light-openid = { version = "1.0.1", features = ["crypto-wrapper"] }
lazy_static = "1.4.0" lazy_static = "1.4.0"
actix-web = "4" actix-web = "4"
actix-remote-ip = "0.1.0" actix-remote-ip = "0.1.0"
@ -22,3 +22,5 @@ futures-util = "0.3.28"
anyhow = "1.0.75" anyhow = "1.0.75"
actix-multipart = "0.6.1" actix-multipart = "0.6.1"
tempfile = "3.8.0" tempfile = "3.8.0"
reqwest = { version = "0.11.18", features = ["stream"] }
url = "2.4.0"

View File

@ -17,7 +17,11 @@ pub const ROUTES_WITHOUT_AUTH: [&str; 5] = [
]; ];
/// Allowed ISO mimetypes /// Allowed ISO mimetypes
pub const ALLOWED_ISO_MIME_TYPES: [&str; 1] = ["application/x-cd-image"]; pub const ALLOWED_ISO_MIME_TYPES: [&str; 3] = [
"application/x-cd-image",
"application/x-iso9660-image",
"application/octet-stream",
];
/// ISO max size /// ISO max size
pub const ISO_MAX_SIZE: usize = 10 * 1000 * 1000 * 1000; pub const ISO_MAX_SIZE: usize = 10 * 1000 * 1000 * 1000;

View File

@ -4,7 +4,10 @@ use crate::controllers::HttpResult;
use crate::utils::files_utils; use crate::utils::files_utils;
use actix_multipart::form::tempfile::TempFile; use actix_multipart::form::tempfile::TempFile;
use actix_multipart::form::MultipartForm; use actix_multipart::form::MultipartForm;
use actix_web::HttpResponse; use actix_web::{web, HttpResponse};
use futures_util::StreamExt;
use std::fs::File;
use std::io::Write;
#[derive(Debug, MultipartForm)] #[derive(Debug, MultipartForm)]
pub struct UploadIsoForm { pub struct UploadIsoForm {
@ -58,3 +61,47 @@ pub async fn upload_file(MultipartForm(mut form): MultipartForm<UploadIsoForm>)
Ok(HttpResponse::Accepted().finish()) Ok(HttpResponse::Accepted().finish())
} }
#[derive(serde::Deserialize)]
pub struct DownloadFromURLReq {
url: String,
filename: String,
}
/// Upload ISO file from URL
pub async fn upload_from_url(req: web::Json<DownloadFromURLReq>) -> HttpResult {
if !files_utils::check_file_name(&req.filename) || !req.filename.ends_with(".iso") {
return Ok(HttpResponse::BadRequest().json("Invalid file name!"));
}
let dest_file = AppConfig::get().iso_storage_path().join(&req.filename);
if dest_file.exists() {
return Ok(HttpResponse::Conflict().json("A similar file already exists!"));
}
let response = reqwest::get(&req.url).await?;
if let Some(len) = response.content_length() {
if len > constants::ISO_MAX_SIZE as u64 {
return Ok(HttpResponse::BadRequest().json("File is too large!"));
}
}
if let Some(ct) = response.headers().get("content-type") {
if !constants::ALLOWED_ISO_MIME_TYPES.contains(&ct.to_str()?) {
return Ok(HttpResponse::BadRequest().json("Invalid file mimetype!"));
}
}
let mut stream = response.bytes_stream();
let mut file = File::create(dest_file)?;
while let Some(item) = stream.next().await {
let bytes = item?;
file.write_all(&bytes)?;
}
Ok(HttpResponse::Accepted().finish())
}

View File

@ -65,4 +65,16 @@ impl From<tempfile::PersistError> for HttpErr {
} }
} }
impl From<reqwest::Error> for HttpErr {
fn from(value: reqwest::Error) -> Self {
HttpErr { err: value.into() }
}
}
impl From<reqwest::header::ToStrError> for HttpErr {
fn from(value: reqwest::header::ToStrError) -> Self {
HttpErr { err: value.into() }
}
}
pub type HttpResult = Result<HttpResponse, HttpErr>; pub type HttpResult = Result<HttpResponse, HttpErr>;

View File

@ -104,6 +104,10 @@ async fn main() -> std::io::Result<()> {
"/api/iso/upload", "/api/iso/upload",
web::post().to(iso_controller::upload_file), web::post().to(iso_controller::upload_file),
) )
.route(
"/api/iso/upload_from_url",
web::post().to(iso_controller::upload_from_url),
)
}) })
.bind(&AppConfig::get().listen_address)? .bind(&AppConfig::get().listen_address)?
.run() .run()

View File

@ -1 +1,2 @@
pub mod files_utils; pub mod files_utils;
pub mod url_utils;

View File

@ -0,0 +1,58 @@
use std::fmt::Display;
use url::Url;
/// Check out whether a URL is valid or not
pub fn check_url(url: impl Display) -> bool {
match Url::parse(&url.to_string()) {
Ok(u) => {
if u.scheme() != "http" && u.scheme() != "https" {
log::debug!("URL is invalid, scheme is not http or https!");
return false;
}
if u.port_or_known_default() != Some(443) && u.port_or_known_default() != Some(80) {
log::debug!("URL is invalid, port is not 80 or 443!");
return false;
}
true
}
Err(e) => {
log::debug!("URL is invalid, could not be parsed! {e}");
false
}
}
}
#[cfg(test)]
mod test {
use crate::utils::url_utils::check_url;
#[test]
fn valid_url_ubuntu() {
assert!(check_url(
"https://releases.ubuntu.com/22.04.3/ubuntu-22.04.3-desktop-amd64.iso"
));
}
#[test]
fn valid_url_with_port_ubuntu() {
assert!(check_url(
"https://releases.ubuntu.com:443/22.04.3/ubuntu-22.04.3-desktop-amd64.iso"
));
}
#[test]
fn invalid_valid_url_ftp() {
assert!(!check_url(
"ftp://releases.ubuntu.com/22.04.3/ubuntu-22.04.3-desktop-amd64.iso"
));
}
#[test]
fn invalid_valid_bad_port() {
assert!(!check_url(
"http://releases.ubuntu.com:81/22.04.3/ubuntu-22.04.3-desktop-amd64.iso"
));
}
}

View File

@ -18,4 +18,15 @@ export class IsoFilesApi {
progress: progress, progress: progress,
}); });
} }
/**
* Upload iso from URL
*/
static async UploadFromURL(url: string, filename: string): Promise<void> {
await APIClient.exec({
method: "POST",
uri: "/iso/upload_from_url",
jsonData: { url: url, filename: filename },
});
}
} }

View File

@ -1,4 +1,4 @@
import { Button, LinearProgress, Typography } from "@mui/material"; import { Button, LinearProgress, TextField, Typography } from "@mui/material";
import { filesize } from "filesize"; import { filesize } from "filesize";
import { MuiFileInput } from "mui-file-input"; import { MuiFileInput } from "mui-file-input";
import React from "react"; import React from "react";
@ -8,16 +8,20 @@ import { useAlert } from "../hooks/providers/AlertDialogProvider";
import { useSnackbar } from "../hooks/providers/SnackbarProvider"; import { useSnackbar } from "../hooks/providers/SnackbarProvider";
import { VirtWebPaper } from "../widgets/VirtWebPaper"; import { VirtWebPaper } from "../widgets/VirtWebPaper";
import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer"; import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer";
import { useLoadingMessage } from "../hooks/providers/LoadingMessageProvider";
export function IsoFilesRoute(): React.ReactElement { export function IsoFilesRoute(): React.ReactElement {
return ( return (
<VirtWebRouteContainer label="ISO files management"> <VirtWebRouteContainer label="ISO files management">
<UploadIsoFileForm onFileUploaded={() => alert("file uploaded!")} /> <UploadIsoFileCard onFileUploaded={() => alert("file uploaded!")} />
<UploadIsoFileFromUrlCard
onFileUploaded={() => alert("file uploaded!")}
/>
</VirtWebRouteContainer> </VirtWebRouteContainer>
); );
} }
function UploadIsoFileForm(p: { function UploadIsoFileCard(p: {
onFileUploaded: () => void; onFileUploaded: () => void;
}): React.ReactElement { }): React.ReactElement {
const alert = useAlert(); const alert = useAlert();
@ -53,6 +57,8 @@ function UploadIsoFileForm(p: {
setValue(null); setValue(null);
snackbar("The file was successfully uploaded!"); snackbar("The file was successfully uploaded!");
p.onFileUploaded();
} catch (e) { } catch (e) {
console.error(e); console.error(e);
await alert("Failed to perform file upload! " + e); await alert("Failed to perform file upload! " + e);
@ -75,7 +81,6 @@ function UploadIsoFileForm(p: {
return ( return (
<VirtWebPaper label="File upload"> <VirtWebPaper label="File upload">
<div style={{ display: "flex", alignItems: "center" }}> <div style={{ display: "flex", alignItems: "center" }}>
<span style={{ width: "10px" }}></span>
<MuiFileInput <MuiFileInput
value={value} value={value}
onChange={handleChange} onChange={handleChange}
@ -88,3 +93,55 @@ function UploadIsoFileForm(p: {
</VirtWebPaper> </VirtWebPaper>
); );
} }
function UploadIsoFileFromUrlCard(p: {
onFileUploaded: () => void;
}): React.ReactElement {
const alert = useAlert();
const snackbar = useSnackbar();
const loadingMessage = useLoadingMessage();
const [url, setURL] = React.useState("");
const [filename, setFilename] = React.useState<null | string>(null);
const autoFileName = url.split("/").slice(-1)[0];
const actualFileName = filename ?? autoFileName;
const upload = async () => {
try {
loadingMessage.show("Downloading file from URL...");
await IsoFilesApi.UploadFromURL(url, actualFileName);
setURL("");
setFilename(null);
snackbar("Successfully downloaded file!");
} catch (e) {
console.error(e);
alert("Failed to download file!");
}
loadingMessage.hide();
};
return (
<VirtWebPaper label="File upload from URL">
<div style={{ display: "flex", alignItems: "center" }}>
<TextField
label="URL"
value={url}
style={{ flex: 3 }}
onChange={(e) => setURL(e.target.value)}
/>
<span style={{ width: "10px" }}></span>
<TextField
label="Filename"
value={actualFileName}
style={{ flex: 2 }}
onChange={(e) => setFilename(e.target.value)}
/>
{url !== "" && actualFileName !== "" && (
<Button onClick={upload}>Upload file</Button>
)}
</div>
</VirtWebPaper>
);
}

View File

@ -5,7 +5,7 @@ export function VirtWebPaper(
p: { label: string } & PropsWithChildren p: { label: string } & PropsWithChildren
): React.ReactElement { ): React.ReactElement {
return ( return (
<Paper elevation={2} style={{ padding: "10px" }}> <Paper elevation={2} style={{ padding: "10px", margin: "20px" }}>
<Typography <Typography
variant="subtitle1" variant="subtitle1"
style={{ marginBottom: "10px", fontWeight: "bold" }} style={{ marginBottom: "10px", fontWeight: "bold" }}