let _ = self.tx.send(cmd);
}
+ pub fn save_attachment_file<F>(&self, attachment_id: i32, path: &Path, callback: F)
+ where F: FnOnce(FileResult) + Send + 'static {
+ let backend = self.backend.clone();
+ let pathbuf = path.to_path_buf();
+ let cmd = Box::pin(async move {
+ let content = async {
+ attachmentcontent::Entity::find()
+ .filter(attachmentcontent::Column::AttachmentId.eq(attachment_id))
+ .one(&backend.db)
+ .await?
+ .ok_or(DbErr::RecordNotFound(format!("attachment {} not found", attachment_id)))
+ }.await;
+ let result = match content {
+ Ok(content) => {
+ match write_file(pathbuf.as_path(), &content.content).await {
+ Ok(()) => FileResult::Ok,
+ Err(err) => FileResult::FileError(err),
+ }
+ },
+ Err(e) => {
+ FileResult::DbErr(e)
+ }
+ };
+ callback(result);
+ });
+ let _ = self.tx.send(cmd);
+ }
+
pub fn open_extern<F>(&self, note_id: i32, callback: F)
where F: FnOnce(OpenExternResult) + Send + 'static {
let backend = self.backend.clone();
ValidationError(&'static str)
}
+#[derive(Debug)]
+pub enum FileResult {
+ Ok,
+ DbErr(DbErr),
+ FileError(Error)
+}
+
pub struct SaveNoteRet {
pub model: entity::note::Model,
pub content_id: i32,
return None
}
-pub async fn write_file(path: &Path, content: &str) -> std::io::Result<()> {
+pub async fn write_file(path: &Path, content: &[u8]) -> std::io::Result<()> {
let mut file = match File::create(path).await {
Ok(f) => f,
Err(err) if err.kind() == ErrorKind::NotFound => {
Err(err) => return Err(err),
};
- file.write_all(content.as_bytes()).await?;
+ file.write_all(content).await?;
Ok(())
}
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
+use std::path::Path;
use backend::backend::BackendHandle;
use ui_rs::ui::*;
use ui_rs::{action, button, imageviewer, tabview, ui_actions, webview, UiModel};
hb.start(|obj|{
button!(obj, icon = UiIconSet::GoBack.as_str(), action = "go_back");
button!(obj, icon = UiIconSet::GoForward.as_str(), action = "go_forward");
+ button!(obj, icon = UiIconSet::SaveLocal.as_str(), action = "save_as");
});
});
self.update_selection(selected_index);
}
}
+
+ #[action]
+ pub fn save_as(&mut self, _event: &ActionEvent) -> Option<()> {
+ let obj = self.obj.get_object()?;
+ let attachment = self.attachments.get(self.selected_index?)?;
+ let attachment_id = attachment.attachment.attachment_id;
+ obj.savefile_dialog(attachment.attachment.nodename.as_str(), move|event|{
+ if let EventType::FileList(files) = event.event_type && let Some(path_str) = files.get(0) {
+ let path = Path::new(path_str);
+ event.data.backend.save_attachment_file(attachment_id, path, |result|{
+ println!("save attachment file: {:?}", result);
+ });
+ }
+ });
+
+ Some(())
+ }
}