Skip to content

Commit

Permalink
Merge pull request #57 from Berrysoft/dev/tar
Browse files Browse the repository at this point in the history
Add vfs support to runtime.
  • Loading branch information
Berrysoft authored Dec 16, 2022
2 parents b94fd44 + 400fd03 commit 815dd64
Show file tree
Hide file tree
Showing 52 changed files with 679 additions and 344 deletions.
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@ release-cross:
EXAMPLES:=Basic Fibonacci Fibonacci2 Gacha Live2D Orga Styles

define example-tpl
.PHONY: example-$(1) example-$(1)-gui example-$(1)-release example-$(1)-gui-release examples/$(1)/config.tex
.PHONY: example-$(1) example-$(1)-gui examples/$(1)/config.tex examples/$(1).frfs
example-$(1): examples/$(1)/config.yaml plugins
cd bins && $$(MAKE) run FILE=$$(realpath $$<)
example-$(1)-gui: examples/$(1)/config.yaml plugins
example-$(1)-gui: examples/$(1).frfs plugins
cd bins && $$(MAKE) run-gui FILE=$$(realpath $$<)
example-$(1)-release: examples/$(1)/config.yaml plugins release
bins/target/release/ayaka-check $$< --auto
example-$(1)-gui-release: examples/$(1)/config.yaml plugins release
bins/target/release/ayaka-gui $$<
examples/$(1)/config.tex: examples/$(1)/config.yaml plugins
cd bins && $$(MAKE) run-latex FILE=$$(realpath $$<)
examples/$(1).frfs:
frfs pack examples/$(1)/ examples/$(1).frfs

endef

Expand Down
112 changes: 47 additions & 65 deletions bins/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion bins/ayaka-gui/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2", features = ["cli", "window-all"] }
tauri-plugin-window-state = "0.1"
actix-web = "4"
actix-files = "0.6"
actix-cors = "0.6"
trylog = "0.3"
tryiterator = { git = "https://github.com/Pernosco/tryiterator.git" }
vfs = "0.8"
mime_guess = "2.0"
stream-future = "0.3"

[features]
default = [ "custom-protocol" ]
Expand Down
43 changes: 35 additions & 8 deletions bins/ayaka-gui/src-tauri/src/asset_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
use actix_cors::Cors;
use actix_files::NamedFile;
use actix_web::{
http::header::CONTENT_TYPE, middleware::Logger, web, App, HttpRequest, HttpResponse,
HttpServer, Responder, Scope,
http::header::CONTENT_TYPE,
middleware::Logger,
web::{self, Bytes},
App, HttpRequest, HttpResponse, HttpServer, Responder, Scope,
};
use std::{net::TcpListener, path::PathBuf, sync::OnceLock};
use std::{io::Result, net::TcpListener, sync::OnceLock};
use stream_future::try_stream;
use tauri::{
plugin::{Builder, TauriPlugin},
AppHandle, Runtime,
};
use vfs::*;

pub(crate) static ROOT_PATH: OnceLock<PathBuf> = OnceLock::new();
pub(crate) static ROOT_PATH: OnceLock<VfsPath> = OnceLock::new();
const BUFFER_LEN: usize = 65536;

#[try_stream(Bytes)]
fn file_stream(mut file: Box<dyn SeekAndRead>, length: usize) -> Result<()> {
let length = length.min(BUFFER_LEN);
loop {
let mut buffer = vec![0; length];
let read_bytes = file.read(&mut buffer)?;
buffer.truncate(read_bytes);
if read_bytes > 0 {
yield Bytes::from(buffer);
} else {
break;
}
}
Ok(())
}

async fn fs_resolver(req: HttpRequest) -> impl Responder {
let url = req.uri().path().strip_prefix("/fs/").unwrap_or_default();
let path = ROOT_PATH.get().unwrap().join(url);
if let Ok(file) = NamedFile::open_async(&path).await {
file.into_response(&req)
let path = ROOT_PATH.get().unwrap().join(url).unwrap();
if let Ok(file) = path.open_file() {
let length = path
.metadata()
.map(|meta| meta.len as usize)
.unwrap_or(BUFFER_LEN);
let mime = mime_guess::from_path(path.as_str()).first_or_octet_stream();
HttpResponse::Ok()
.content_type(mime)
.streaming(file_stream(file, length))
} else {
HttpResponse::NotFound().finish()
}
Expand Down
Loading

0 comments on commit 815dd64

Please sign in to comment.