Raise upload limit to 25 MiB and run rustfmt
This commit is contained in:
parent
27e8c525ed
commit
4e52ce05d7
|
@ -1,12 +1,12 @@
|
|||
use std::io::Read;
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use multipart::client::lazy::Multipart;
|
||||
use std::error::Error;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct DiscordJsonError {
|
||||
invalid_json: String
|
||||
invalid_json: String,
|
||||
}
|
||||
|
||||
impl Display for DiscordJsonError {
|
||||
|
@ -26,7 +26,7 @@ impl Error for DiscordJsonError {}
|
|||
struct CountingRead<'a, T, F> {
|
||||
inner_stream: &'a mut T,
|
||||
current_progress: u64,
|
||||
progress_callback: F
|
||||
progress_callback: F,
|
||||
}
|
||||
|
||||
impl<'a, T: Read, F: FnMut(u64)> CountingRead<'a, T, F> {
|
||||
|
@ -34,7 +34,7 @@ impl<'a, T: Read, F: FnMut(u64)> CountingRead<'a, T, F> {
|
|||
Self {
|
||||
inner_stream,
|
||||
current_progress: 0,
|
||||
progress_callback
|
||||
progress_callback,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,12 @@ impl<T: Read, F: FnMut(u64)> Read for CountingRead<'_, T, F> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn upload_discord<F: FnMut(f64)>(filename: &str, data: &[u8], webhook: &str, mut percent_callback: F) -> anyhow::Result<String> {
|
||||
pub fn upload_discord<F: FnMut(f64)>(
|
||||
filename: &str,
|
||||
data: &[u8],
|
||||
webhook: &str,
|
||||
mut percent_callback: F,
|
||||
) -> anyhow::Result<String> {
|
||||
let mut cursor = io::Cursor::new(data);
|
||||
let mut mp = Multipart::new();
|
||||
let counting_reader = CountingRead::new(&mut cursor, move |bytes| {
|
||||
|
@ -59,9 +64,14 @@ pub fn upload_discord<F: FnMut(f64)>(filename: &str, data: &[u8], webhook: &str,
|
|||
mp.add_stream("file", counting_reader, Some(filename.to_string()), None);
|
||||
let mpdata = mp.prepare()?;
|
||||
let response = ureq::post(webhook)
|
||||
.set("Content-Type", &format!("multipart/form-data; boundary={}", mpdata.boundary()))
|
||||
.set(
|
||||
"Content-Type",
|
||||
&format!("multipart/form-data; boundary={}", mpdata.boundary()),
|
||||
)
|
||||
.send(mpdata)?;
|
||||
let response_string = response.into_string()?;
|
||||
let mut rjson = json::parse(&response_string)?;
|
||||
Ok(rjson["attachments"][0]["url"].take_string().ok_or_else(|| DiscordJsonError::new(response_string.clone()))?)
|
||||
}
|
||||
Ok(rjson["attachments"][0]["url"]
|
||||
.take_string()
|
||||
.ok_or_else(|| DiscordJsonError::new(response_string.clone()))?)
|
||||
}
|
||||
|
|
95
src/main.rs
95
src/main.rs
|
@ -1,34 +1,49 @@
|
|||
use gtk::*;
|
||||
use gtk::prelude::*;
|
||||
use gtk::glib::clone;
|
||||
use glib::Sender;
|
||||
use std::fs::File;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::io::{SeekFrom, Seek, Read};
|
||||
use anyhow::anyhow;
|
||||
use crate::discord::upload_discord;
|
||||
use anyhow::anyhow;
|
||||
use glib::Sender;
|
||||
use gtk::glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk::*;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
use std::path::Path;
|
||||
|
||||
mod discord;
|
||||
const MAXBUF: u64 = 8_380_416;
|
||||
const MAXBUF: u64 = 26_214_400; // 25 MiB
|
||||
|
||||
fn main() {
|
||||
let app = Application::builder().application_id("moe.lemonsh.zdiu").build();
|
||||
let app = Application::builder()
|
||||
.application_id("moe.lemonsh.zdiu")
|
||||
.build();
|
||||
app.connect_activate(build_ui);
|
||||
app.run();
|
||||
}
|
||||
|
||||
fn errormsg<P: IsA<Window>>(text: &str, parent: Option<&P>) {
|
||||
let dialog = MessageDialog::new(parent, DialogFlags::MODAL, MessageType::Error, ButtonsType::Ok, text);
|
||||
let dialog = MessageDialog::new(
|
||||
parent,
|
||||
DialogFlags::MODAL,
|
||||
MessageType::Error,
|
||||
ButtonsType::Ok,
|
||||
text,
|
||||
);
|
||||
dialog.run();
|
||||
dialog.close();
|
||||
}
|
||||
|
||||
enum UiThreadTask {
|
||||
UpdateProgress(f64), Finalize(anyhow::Result<String>)
|
||||
UpdateProgress(f64),
|
||||
Finalize(anyhow::Result<String>),
|
||||
}
|
||||
|
||||
fn upload_file_task(tx: &Sender<UiThreadTask>, mut file: File, size: u64, name: &str, webhook: &str) -> anyhow::Result<String> {
|
||||
fn upload_file_task(
|
||||
tx: &Sender<UiThreadTask>,
|
||||
mut file: File,
|
||||
size: u64,
|
||||
name: &str,
|
||||
webhook: &str,
|
||||
) -> anyhow::Result<String> {
|
||||
let mut buffer = vec![0_u8; size as usize];
|
||||
file.read_exact(&mut buffer[..])?;
|
||||
upload_discord(name, &buffer, webhook, move |fr| {
|
||||
|
@ -36,7 +51,12 @@ fn upload_file_task(tx: &Sender<UiThreadTask>, mut file: File, size: u64, name:
|
|||
})
|
||||
}
|
||||
|
||||
fn lock_gui(file_upload_button: &FileChooserButton, clip_upload_button: &Button, webhook_control: &Button, progress: &ProgressBar) {
|
||||
fn lock_gui(
|
||||
file_upload_button: &FileChooserButton,
|
||||
clip_upload_button: &Button,
|
||||
webhook_control: &Button,
|
||||
progress: &ProgressBar,
|
||||
) {
|
||||
file_upload_button.set_sensitive(false);
|
||||
clip_upload_button.set_sensitive(false);
|
||||
webhook_control.set_sensitive(false);
|
||||
|
@ -152,30 +172,33 @@ fn build_ui(app: &Application) {
|
|||
}
|
||||
}));
|
||||
|
||||
rx.attach(None, clone!(@strong window => move |data| {
|
||||
match data {
|
||||
UiThreadTask::UpdateProgress(p) => {
|
||||
progress.set_fraction(p);
|
||||
progress.set_text(Some(format!("{:.1}%", p*100.0).as_str()));
|
||||
}
|
||||
UiThreadTask::Finalize(u) => {
|
||||
file_upload_button.set_sensitive(true);
|
||||
clip_upload_button.set_sensitive(true);
|
||||
webhook_control.set_sensitive(true);
|
||||
progress.set_fraction(0.0);
|
||||
progress.set_text(Some("0%"));
|
||||
match u {
|
||||
Ok(o) => {
|
||||
output_field.set_text(o.as_str());
|
||||
}
|
||||
Err(e) => {
|
||||
errormsg(e.to_string().as_str(), Some(&window));
|
||||
rx.attach(
|
||||
None,
|
||||
clone!(@strong window => move |data| {
|
||||
match data {
|
||||
UiThreadTask::UpdateProgress(p) => {
|
||||
progress.set_fraction(p);
|
||||
progress.set_text(Some(format!("{:.1}%", p*100.0).as_str()));
|
||||
}
|
||||
UiThreadTask::Finalize(u) => {
|
||||
file_upload_button.set_sensitive(true);
|
||||
clip_upload_button.set_sensitive(true);
|
||||
webhook_control.set_sensitive(true);
|
||||
progress.set_fraction(0.0);
|
||||
progress.set_text(Some("0%"));
|
||||
match u {
|
||||
Ok(o) => {
|
||||
output_field.set_text(o.as_str());
|
||||
}
|
||||
Err(e) => {
|
||||
errormsg(e.to_string().as_str(), Some(&window));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
glib::Continue(true)
|
||||
}));
|
||||
glib::Continue(true)
|
||||
}),
|
||||
);
|
||||
window.set_application(Some(app));
|
||||
window.present();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue