upload.rs
1.97 KB
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
use mogwai::{prelude::*, utils::window};
use web_sys::{File, ImageBitmap, ReadableStream};
use super::{view::upload_preview_view, logic::{upload_preview_logic, UploadLogic}};
#[derive(Clone, Debug)]
pub(crate) struct Upload {
pub(super) id :usize,
file :File,
bitmap :ImageBitmap,
pub(super) tx_logic :broadcast::Sender<UploadLogic>,
}
impl Upload {
pub(super) async fn new( id :usize
, file :File
, tx_logic :broadcast::Sender<UploadLogic>
) -> Upload {
let bitmap = window()
. create_image_bitmap_with_blob(&file.clone().into())
. unwrap();
let bitmap = JsFuture::from(bitmap)
. await.unwrap()
. dyn_into::<ImageBitmap>().unwrap();
Self { id, file, bitmap, tx_logic }
}
pub(crate) fn mime_type(&self) -> String {
self.file.type_()
}
pub(crate) fn data(&self) -> ReadableStream {
self.file.stream()
}
pub(super) fn bitmap(&self) -> ImageBitmap {
self.to_owned().bitmap
}
}
impl From<Upload> for Component<Dom> {
fn from(upload: Upload) -> Self {
let (tx_canvas, rx_canvas) = broadcast::bounded(1);
let (tx_click, rx_click) = broadcast::bounded(1);
let view = upload_preview_view( tx_canvas
, tx_click
, upload.file.name()
, upload.file.size()
, upload.file.type_()
, upload.file.last_modified() );
let logic = upload_preview_logic(rx_canvas, rx_click, upload);
Component::from(view).with_logic(logic)
}
}
impl From<Upload> for ViewBuilder<Dom> {
fn from(upload: Upload) -> Self {
let component :Component<Dom> = upload.into();
component.into()
}
}