Skip to content

Commit

Permalink
model list impl
Browse files Browse the repository at this point in the history
  • Loading branch information
cordx56 committed Jan 22, 2025
1 parent 1da7829 commit dcf312c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
34 changes: 31 additions & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use mouse_position::mouse_position::Mouse;
use std::path::PathBuf;
use tauri::Manager;
use window_shadows::set_shadow;

#[derive(serde::Serialize, Clone)]
struct MousePosition {
Expand All @@ -18,6 +17,10 @@ struct CursorGrab {
struct ModelViewParam {
vrm: String,
}
#[derive(serde::Serialize, Clone)]
struct ModelList {
models: Vec<String>,
}

fn main() {
//simple_logger::init_with_env().unwrap();
Expand Down Expand Up @@ -75,15 +78,40 @@ fn main() {

let app_handle = app.app_handle();
tauri::async_runtime::spawn(async move {
use axum::{
http::StatusCode,
routing::{get, post},
Json,
};
use tower_http::cors::{AllowMethods, Any, CorsLayer};

let data_dir = app_handle.path_resolver().app_data_dir().unwrap();
let data_dir2 = data_dir.clone();
let data_dir3 = data_dir.clone();

let app = axum::Router::new()
.route(
"/vrm",
get(|| async move {
let vrm_dir = data_dir3.join("vrm");
if let Ok(mut files) = tokio::fs::read_dir(&vrm_dir).await {
let mut models = Vec::new();
while let Ok(Some(file)) = files.next_entry().await {
let filename = file.file_name().to_string_lossy().to_string();
if filename.ends_with(".vrm") {
models.push(filename);
}
}
(StatusCode::OK, Json(ModelList { models }))
} else {
let _ = tokio::fs::create_dir_all(&vrm_dir).await;
(StatusCode::OK, Json(ModelList { models: Vec::new() }))
}
}),
)
.route(
"/vrm/{vrm}",
axum::routing::get(|p: axum::extract::Path<String>| async move {
get(|p: axum::extract::Path<String>| async move {
if let Ok(data) = tokio::fs::read(
data_dir
.join("vrm")
Expand All @@ -99,7 +127,7 @@ fn main() {
)
.route(
"/vrm/{vrm}",
axum::routing::post(
post(
|p: axum::extract::Path<String>, body: axum::body::Bytes| async move {
if let Ok(_) = tokio::fs::write(
data_dir2
Expand Down
14 changes: 3 additions & 11 deletions src/components/VrmLibrary.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useState, useEffect, ChangeEventHandler } from "react";
import { readDir, createDir, exists } from "@tauri-apps/api/fs";
import { vrmDir } from "@/util/path";
import { writeModel } from "@/util/fetch";
import { listModel, writeModel } from "@/util/fetch";

type Props = {
onSelect: (path: string) => any;
Expand All @@ -11,14 +9,8 @@ export default ({ onSelect }: Props) => {
const [models, setModels] = useState<string[]>([]);
const [processing, setProcessing] = useState(false);
const readModels = async () => {
if (!(await exists(vrmDir))) {
await createDir(vrmDir, { recursive: true });
}
const read = await readDir(vrmDir);
const vrmFiles = read
.filter((p) => p.path.endsWith(".vrm") && typeof p.name === "string")
.map((p) => p.name!);
setModels(vrmFiles);
const models = await listModel();
setModels(models);
};
useEffect(() => {
readModels();
Expand Down
9 changes: 9 additions & 0 deletions src/util/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export const listModel = async (): Promise<string[]> => {
const resp = await fetch(new URL(`http://localhost:8108/vrm`));
if (resp.ok) {
return (await resp.json()).models;
} else {
return [];
}
};

export const readModel = async (name: string): Promise<Uint8Array | null> => {
const resp = await fetch(new URL(`http://localhost:8108/vrm/${name}`));
if (resp.ok) {
Expand Down
4 changes: 0 additions & 4 deletions src/util/path.ts

This file was deleted.

0 comments on commit dcf312c

Please sign in to comment.