" />

最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

node管理統(tǒng)計文件大小并顯示目錄磁盤空間狀態(tài)從零實現(xiàn)

 更新時間:2023年12月21日 15:29:36   作者:寒露  
這篇文章主要為大家介紹了node管理統(tǒng)計文件大小并顯示目錄磁盤空間狀態(tài)的從零實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

explorer-manager

新增依賴

pnpm i node-df get-folder-size
  • node-df 執(zhí)行 linux 的 df 命令,并將內(nèi)容格式化為 node 可直接使用結(jié)構(gòu)
  • get-folder-size 快速統(tǒng)計文件夾占用大小

創(chuàng)建對應方法

分析文件夾大小

import getFolderSize from 'get-folder-size'
export const getFolderSizeAction = async (path) => {
  return await getFolderSize.loose(formatPath(path))
}

執(zhí)行 df 命令文件

explorer-manager/src/df.mjs

import df from 'node-df'
import { formatPath } from './format-path.mjs'
/**
 *
 * @param {import('./type').DfOptType} opt
 * @returns {Promise<import('./type').DfResItemType[]>}
 */
export const getDF = async (opt = {}) => {
  return new Promise((res, rej) => {
    df(opt, (error, response) => {
      if (error) {
        rej(error)
      }
      res(response)
    })
  })
}
export const findDfInfo = async (path = '') => {
  const info = await getDF()
  const join_path = formatPath(path)
  return info
    .filter((item) => {
      return join_path.includes(item.mount)
    })
    .pop()
}

對應 type 文件

export type DfResItemType = {
  filesystem: string
  size: number
  used: number
  available: number
  capacity: number
  mount: string
}
export type DfOptType = Partial<{
  file: string
  prefixMultiplier: 'KiB|MiB|GiB|TiB|PiB|EiB|ZiB|YiB|MB|GB|TB|PB|EB|ZB|YB'
  isDisplayPrefixMultiplier: boolean
  precision: number
}>

explorer

讀取文件夾大小,一個按鈕放置在點擊卡片右上角的 “…” 的下拉菜單內(nèi)的“信息”菜單內(nèi)。一個位于卡片視圖的左下角,有個icon,點擊后計算當前文件夾大小。

讀取文件夾大小

創(chuàng)建 floder-size 組件,

里面包含一個 FolderSize 組件,用于顯示完整文案 “文件夾大小:[size]”

一個 FolderSizeBtn,用于點擊時加載 size 文案

'use client'
import React, { useState } from 'react'
import { useRequest } from 'ahooks'
import axios, { AxiosRequestConfig } from 'axios'
import { ResType } from '@/app/path/api/get-folder-size/route'
import Bit from '@/components/bit'
import { LoadingOutlined, ReloadOutlined } from '@ant-design/icons'
import { Button } from 'antd'
const getFolderSize = (config: AxiosRequestConfig) => axios.get<ResType>('/path/api/get-folder-size', config)
const useGetFolderSize = (path: string) => {
  const { data: size, loading } = useRequest(() =>
    getFolderSize({ params: { path: path } }).then(({ data }) => {
      return data.data
    }),
  )
  return { size, loading }
}
const FolderSize: React.FC<{ path: string; title?: string | null }> = ({ path, title = '文件夾大小' }) => {
  const { size, loading } = useGetFolderSize(path)
  return <>{loading ? <LoadingOutlined /> : <Bit title={title}>{size}</Bit>}</>
}
export const FolderSizeBtn: React.FC<{ path: string }> = ({ path }) => {
  const [show, changeShow] = useState(false)
  return (
    <>
      {show ? (
        <FolderSize path={path} title={null} />
      ) : (
        <Button icon={<ReloadOutlined />} onClick={() => changeShow(true)} />
      )}
    </>
  )
}
export default FolderSize

加入 下拉菜單中

if (item.is_directory || is_show_img_exif) {
    menu.items?.push({
      icon: <InfoOutlined />,
      label: '信息',
      key: 'info',
      onClick: () => {
        if (item.is_directory) {
          modal.info({ title: path, content: <FolderSize path={path} />, width: 500 })
        } else {
          changeImgExif(preview_path)
        }
      },
    })
  }

判斷當是目錄時,直接彈出 modal.info 窗口,內(nèi)容為 FolderSize 組件。

card-display.tsx 加入下面修改

...
import { FolderSizeBtn } from '@/components/folder-size'
import { useReplacePathname } from '@/components/use-replace-pathname'
const CardDisplay: React.FC = () => {
...
  const { joinSearchPath, joinPath } = useReplacePathname()
  return (
...
                  <Flex flex="1 0 auto" style={{ marginRight: 20 }}>
                    {item.is_directory ? (
                      <FolderSizeBtn path={joinSearchPath(item.name)} />
                    ) : (
                      <Bit>{item.stat.size}</Bit>
                    )}
                  </Flex>
...
  )
}
export default CardDisplay

顯示當前目錄磁盤空間狀態(tài)

創(chuàng)建上下文文件

'use client'
import createCtx from '@/lib/create-ctx'
import { DfResItemType } from '@/explorer-manager/src/type'
import React, { useEffect } from 'react'
import { useRequest } from 'ahooks'
import axios from 'axios'
import { usePathname } from 'next/navigation'
import Bit from '@/components/bit'
import { Space } from 'antd'
import { useReplacePathname } from '@/components/use-replace-pathname'
export const DfContext = createCtx<DfResItemType | null>(null!)
const UpdateDfInfo: React.FC = () => {
  const pathname = usePathname()
  const { replace_pathname } = useReplacePathname()
  const dispatch = DfContext.useDispatch()
  const { data = null } = useRequest(() =>
    axios
      .get<{ data: DfResItemType }>('/path/api/get-df', { params: { path: replace_pathname } })
      .then(({ data }) => data.data),
  )
  useEffect(() => {
    dispatch(data)
  }, [data, dispatch, pathname])
  return null
}
export const DfDisplay: React.FC = () => {
  const store = DfContext.useStore()
  return (
    <Space split="/">
      <Bit>{store?.available}</Bit>
      <Bit>{store?.size}</Bit>
    </Space>
  )
}
export const DfProvider: React.FC<React.PropsWithChildren> = ({ children }) => {
  return (
    <DfContext.ContextProvider value={null}>
      <UpdateDfInfo />
      {children}
    </DfContext.ContextProvider>
  )
}

分別將 DfProvider 組件插入 公共 explorer/src/app/path/context.tsx 內(nèi)

+import { DfProvider } from '@/components/df-context'

             <VideoPathProvider>
               <ImgExifProvider>
                 <MovePathProvider>
+                  <RenameProvider>
+                    <DfProvider>{children}</DfProvider>
+                  </RenameProvider>
                 </MovePathProvider>
               </ImgExifProvider>
             </VideoPathProvider>

DfDisplay 顯示組件插入 explorer/src/app/path/[[...path]]/layout-footer.tsx 內(nèi)

+import { DfDisplay } from '@/components/df-context'
+import { ReloadReaddirButton } from '@/components/reload-readdir-button'
 const LayoutFooter: React.FC = () => {
   return (
@@ -12,12 +14,20 @@ const LayoutFooter: React.FC = () => {
       <Flex style={{ width: '100%', height: '40px' }} align="center">
         <Flex flex={1}>
           <Space>
+            <Space.Compact>
+              <ReloadReaddirButton />
+
+              <CreateFolderBtn />
+            </Space.Compact>
             <ReaddirCount />
           </Space>
         </Flex>
+        <Flex flex={1} justify="center" align="center">
+          <DfDisplay />
+        </Flex>
+
         <Flex justify="flex-end" flex={1}>
           <Space>
             <ChangeColumn />

效果

git-repo

yangWs29/share-explorer

以上就是node統(tǒng)計文件大小并顯示目錄磁盤空間狀態(tài)從零實現(xiàn)的詳細內(nèi)容,更多關于node統(tǒng)計文件大小磁盤空間的資料請關注腳本之家其它相關文章!

相關文章

  • node?NPM庫增強版globby?Promise使用學習

    node?NPM庫增強版globby?Promise使用學習

    這篇文章主要為大家介紹了node?NPM庫增強版globby?Promise使用學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • pm2 部署 node的三種方法示例

    pm2 部署 node的三種方法示例

    本篇文章主要介紹了pm2 部署 node的三種方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 實現(xiàn)一個完整的Node.js RESTful API的示例

    實現(xiàn)一個完整的Node.js RESTful API的示例

    本篇文章主要介紹了實現(xiàn)一個完整的Node.js RESTful API的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 利用yarn實現(xiàn)一個webpack+react種子

    利用yarn實現(xiàn)一個webpack+react種子

    其實以前就寫過如何使用React-router和Webpack快速構(gòu)建一個react程序。后來發(fā)現(xiàn)版本太老,于是乎最近又重新組織了下結(jié)構(gòu),使用最近發(fā)布的yarn作為包管理工具,介紹下基本安裝步驟,有需要的朋友們下面來一起看看吧。
    2016-10-10
  • 詳解使用Visual Studio Code對Node.js進行斷點調(diào)試

    詳解使用Visual Studio Code對Node.js進行斷點調(diào)試

    這篇文章主要介紹了詳解使用Visual Studio Code對Node.js進行斷點調(diào)試,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Nodejs如何搭建Web服務器

    Nodejs如何搭建Web服務器

    這篇文章主要介紹了Nodejs如何搭建Web服務器,本文教大家使用 Nodejs搭建一個簡單的Web服務器,感興趣的小伙伴們可以參考一下
    2016-03-03
  • module.exports和exports使用誤區(qū)案例分析

    module.exports和exports使用誤區(qū)案例分析

    module.exports和exports使用誤區(qū),使用require()模塊時,得到的永遠都是module.exports指向的對象
    2023-04-04
  • Node.js readline模塊與util模塊的使用

    Node.js readline模塊與util模塊的使用

    本篇文章主要介紹了Node.js readline模塊與util模塊的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Node.js中用D3.js的方法示例

    Node.js中用D3.js的方法示例

    這篇文章主要給大家介紹了在Node.js中用D3.js的方法,文中分別介紹了如何安裝模塊和一小段簡單的示例代碼,有需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • 詳解使用Node.js 將txt文件轉(zhuǎn)為Excel文件

    詳解使用Node.js 將txt文件轉(zhuǎn)為Excel文件

    這篇文章主要介紹了詳解使用Node.js 將txt文件轉(zhuǎn)為Excel文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評論

荥经县| 洪雅县| 宽城| 观塘区| 襄樊市| 喀什市| 图木舒克市| 邵东县| 伊金霍洛旗| 本溪市| 睢宁县| 泰和县| 云霄县| 元阳县| 洛阳市| 托克逊县| 广德县| 砀山县| 闻喜县| 柘荣县| 蒙自县| 潼南县| 汉阴县| 富宁县| 米易县| 常德市| 昆山市| 阿克苏市| 天镇县| 绥滨县| 乌审旗| 彭州市| 荣成市| 汝南县| 大兴区| 巴里| 长岭县| 通州区| 扬中市| 西昌市| 磴口县|