refactor: some problems are optimized

pull/2095/head
ayang 8 months ago
parent 6e1d1167ce
commit a3c049c4e2
No known key found for this signature in database
GPG Key ID: 7CF0B972091D5C0C

@ -1323,7 +1323,9 @@ async function watchImmediate(
}
/**
* Get the size of a file or directory.
* Get the size of a file or directory. For files, the `stat` functions can be used as well.
*
* If `path` is a directory, this function will recursively iterate over every file and every directory inside of `path` and therefore will be very time consuming if used on larger directories.
* @example
* ```typescript
* import { size, BaseDirectory } from '@tauri-apps/plugin-fs';
@ -1332,7 +1334,7 @@ async function watchImmediate(
* console.log(dirSize); // 1024
* ```
*
* @since 2.0.0
* @since 2.1.0
*/
async function size(path: string | URL): Promise<number> {
if (path instanceof URL && path.protocol !== 'file:') {

@ -3,7 +3,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use anyhow::Ok;
use serde::{Deserialize, Serialize, Serializer};
use serde_repr::{Deserialize_repr, Serialize_repr};
use tauri::{
@ -919,7 +918,7 @@ pub async fn size<R: Runtime>(
if metadata.is_file() {
Ok(metadata.len())
} else {
let size = get_dir_size(resolved_path).map_err(|e| {
let size = get_dir_size(&resolved_path).map_err(|e| {
format!(
"failed to get size at path: {} with error: {e}",
resolved_path.display()
@ -930,7 +929,7 @@ pub async fn size<R: Runtime>(
}
}
fn get_dir_size(path: PathBuf) -> CommandResult<u64> {
fn get_dir_size(path: &PathBuf) -> CommandResult<u64> {
let mut size = 0;
for entry in std::fs::read_dir(path)? {
@ -940,7 +939,7 @@ fn get_dir_size(path: PathBuf) -> CommandResult<u64> {
if metadata.is_file() {
size += metadata.len();
} else if metadata.is_dir() {
size += get_dir_size(entry.path())?;
size += get_dir_size(&entry.path())?;
}
}

Loading…
Cancel
Save