Skip to content

Commit

Permalink
feat: 将文件大小的获取由web端转移到rust端
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoJikun committed Nov 22, 2024
1 parent 75594f4 commit ac5949f
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 31 deletions.
12 changes: 8 additions & 4 deletions src-tauri/src/preview.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::mpsc;
use std::{sync::mpsc};
use std::thread;
use tauri::{
webview::PageLoadEvent, AppHandle, Error as TauriError, Manager, WebviewUrl, WebviewWindowBuilder,
Expand Down Expand Up @@ -236,10 +236,12 @@ impl WebRoute {
url.push_str("?");
url.push_str(
format!(
"file_type={}&path={}&extension={}",
"file_type={}&path={}&extension={}&size={}&last_modified={}",
self.query.get_file_type(),
urlencoding::encode(&self.query.get_path()),
self.query.get_extension()
self.query.get_extension(),
self.query.get_size(),
self.query.get_last_modified()
)
.as_str(),
);
Expand Down Expand Up @@ -306,11 +308,13 @@ impl PreviewFile {
pub fn preview_file(app: AppHandle) -> Result<(), TauriError> {
let file_path = Selected::new();
if file_path.is_some() {
let file_info = get_file_info(&file_path.unwrap());
let file_path = file_path.unwrap();
let file_info = get_file_info(&file_path);

if file_info.is_none() {
return Ok(());
}

let file_info = file_info.unwrap();
match app.get_webview_window("preview") {
Some(window) => {
Expand Down
24 changes: 22 additions & 2 deletions src-tauri/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::os::windows::fs::MetadataExt;
use std::path::Path;


#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct File {
file_type: String,
path: String,
extension: String,
size: u64,
last_modified: u64,
}

impl File {
// 构造 File 实例
fn new(file_type: &str, path: String, extension: String) -> File {
fn new(file_type: &str, path: String, extension: String, size: u64, last_modified: u64,) -> File {
File {
file_type: file_type.to_string(),
path,
extension,
size,
last_modified,
}
}

Expand All @@ -33,6 +39,16 @@ impl File {
pub fn get_extension(&self) -> String {
self.extension.clone()
}

// 获取文件大小
pub fn get_size(&self) -> u64 {
self.size
}

// 获取文件最后修改时间
pub fn get_last_modified(&self) -> u64 {
self.last_modified
}
}

pub fn get_file_info(path: &str) -> Option<File> {
Expand All @@ -48,9 +64,13 @@ pub fn get_file_info(path: &str) -> Option<File> {
.extension()
.map_or("txt".to_string(), |ext| ext.to_string_lossy().into_owned());


let metadata = file_path.metadata().unwrap();

// 根据扩展名从映射表中获取文件类型
match file_type_mapping().get(extension.as_str()) {
Some(file_type) => Some(File::new(file_type, path_str, extension)),
Some(file_type) => Some(File::new(file_type, path_str, extension, metadata.file_size(), metadata.last_write_time()),
),
None => None, // 如果没有匹配的文件类型,返回 None
}
}
Expand Down
27 changes: 7 additions & 20 deletions src/components/footer.vue
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
<script setup lang="ts">
import { watch, ref } from 'vue'
import { lstat, type FileInfo } from '@tauri-apps/plugin-fs'
import type { FileInfo as File } from '@/utils/typescript'
import type { FileInfo } from '@/utils/typescript'
import { formatBytes } from '@/utils/index'
interface Props {
file?: File
file?: FileInfo
}
const props = defineProps<Props>()
const fileInfo = ref<FileInfo>()
watch(
() => props.file,
async (val, oldVal) => {
if (val?.path !== oldVal?.path || !oldVal) {
fileInfo.value = await lstat(val?.path as string)
// console.log(fileInfo.value, file)
}
},
)
const { file } = defineProps<Props>()
</script>

<template>
<div class="footer">
<span class="footer-item">文件类型:{{ props.file?.file_type }}</span>
<span class="footer-item">文件格式:{{ props.file?.extension }}</span>
<span class="footer-item">文件大小:{{ fileInfo?.size }}</span>
<span class="footer-item">文件类型:{{ file?.file_type }}</span>
<span class="footer-item">文件格式:{{ file?.extension }}</span>
<span class="footer-item">文件大小:{{ formatBytes(file?.size as number) }}</span>
</div>
</template>

Expand Down
6 changes: 2 additions & 4 deletions src/components/header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const handleMax = () => {
const openByDefault = async () => {
const path = props.file?.path
console.log(props.file)
if (path) {
const result = await open(path)
console.log(result)
Expand All @@ -49,7 +48,6 @@ const openByDefault = async () => {
const openWith = async () => {
const path = props.file?.path
console.log(props.file)
if (path) {
await invoke('show_open_with_dialog', { path })
}
Expand All @@ -70,7 +68,7 @@ const pin = async () => {
<template>
<div class="layout-header" data-tauri-drag-region>
<div class="layout-header-extra">
<div>
<div class="no-seleced">
<slot name="logo">
<img v-if="props.logo" :src="logo" alt="logo" />
</slot>
Expand All @@ -80,7 +78,7 @@ const pin = async () => {
<slot name="menu"></slot>
</div>
</div>
<div class="layout-header-operate">
<div class="layout-header-operate no-selected">
<div class="layout-header-operate-item" @click="pin" :title="`${pined ? '取消固定' : '固定'}`">
<n-icon :size="16"><PinOff16Regular v-if="pined" /><Pin16Regular v-else /></n-icon>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout-preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const props = defineProps<Props>()
<div class="preview-body" v-loading="props.loading">
<slot></slot>
</div>
<Footer :file="props.file" class="preview-footer" />
<Footer class="preview-footer" :file="props.file" />
</div>
</template>

Expand Down
21 changes: 21 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,24 @@ export class Type {
return Type.get(val) === 'bigint'
}
}
/**
* 格式化字节大小
* @param size 字节大小
* @returns
*/
export const formatBytes = (size: number): string => {
const KB = 1024
const MB = KB * 1024
const GB = MB * 1024
const TB = GB * 1024

if (size < MB) {
return `${(size / KB).toFixed(2)} KB`
} else if (size < GB) {
return `${(size / MB).toFixed(2)} MB`
} else if (size < TB) {
return `${(size / GB).toFixed(2)} GB`
} else {
return `${(size / TB).toFixed(2)} TB`
}
}
2 changes: 2 additions & 0 deletions src/utils/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export interface FileInfo {
file_type: string
path: string
extension: string
size: number
last_modified: number
}
1 change: 1 addition & 0 deletions src/views/preview/image.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const init = (path: string) => {
}
onMounted(() => {
fileInfo.value = route.query as unknown as FileInfo
console.log(fileInfo.value)
init(convertFileSrc(fileInfo.value.path))
})
</script>
Expand Down

0 comments on commit ac5949f

Please sign in to comment.