@soga/fileutils 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # no desc
@@ -0,0 +1,41 @@
1
+ import { RecordFtype, RecordType } from '@soga/types';
2
+
3
+ declare const getFileSize: (filePath: string) => Promise<number>;
4
+ declare const getFileinfo: (filepath: string) => Promise<{
5
+ filename: string;
6
+ local_ctime: number;
7
+ local_mtime: number;
8
+ local_btime: number;
9
+ record_type: RecordFtype | RecordType;
10
+ record_ftype: RecordFtype | RecordType;
11
+ size: number;
12
+ }>;
13
+
14
+ declare const isVideoSupport: (format: string) => boolean;
15
+ declare const isAudioAdapt: (codec: string) => boolean;
16
+ declare const isAudioHigh: (codec: string) => boolean;
17
+ declare const isSubtitle: (filepath: string) => boolean;
18
+ declare const isTextFile: (filepath: string, length?: number) => Promise<boolean>;
19
+ declare const isValidFile: (filepath: string) => Promise<boolean>;
20
+ declare const isValidPath: (filepath: string) => Promise<boolean>;
21
+
22
+ declare const getRecordTypes: (filepath: string) => Promise<(RecordFtype | RecordType)[]>;
23
+
24
+ declare function getVideoStandard(width: number, height: number): {
25
+ standard: StandardItem;
26
+ adapts: StandardItem[];
27
+ };
28
+ declare const getStandardInfo: (width: number, height: number) => StandardItem;
29
+ type StandardKey = "h_p360" | "h_p540" | "h_p720" | "h_p1080" | "h_p1440" | "h_k4" | "h_k8" | "h_k16" | "h_k32" | "h_k32plus" | "v_p360" | "v_p540" | "v_p720" | "v_p1080" | "v_p1440" | "v_k4" | "v_k8" | "v_k16" | "v_k32" | "v_k32plus";
30
+ type StandardItem = {
31
+ key: StandardKey;
32
+ is_horizontal: boolean;
33
+ width: number;
34
+ height: number;
35
+ label: string;
36
+ tag: string;
37
+ edge: "width" | "height";
38
+ codec: string;
39
+ };
40
+
41
+ export { type StandardItem, type StandardKey, getFileSize, getFileinfo, getRecordTypes, getStandardInfo, getVideoStandard, isAudioAdapt, isAudioHigh, isSubtitle, isTextFile, isValidFile, isValidPath, isVideoSupport };
@@ -0,0 +1,41 @@
1
+ import { RecordFtype, RecordType } from '@soga/types';
2
+
3
+ declare const getFileSize: (filePath: string) => Promise<number>;
4
+ declare const getFileinfo: (filepath: string) => Promise<{
5
+ filename: string;
6
+ local_ctime: number;
7
+ local_mtime: number;
8
+ local_btime: number;
9
+ record_type: RecordFtype | RecordType;
10
+ record_ftype: RecordFtype | RecordType;
11
+ size: number;
12
+ }>;
13
+
14
+ declare const isVideoSupport: (format: string) => boolean;
15
+ declare const isAudioAdapt: (codec: string) => boolean;
16
+ declare const isAudioHigh: (codec: string) => boolean;
17
+ declare const isSubtitle: (filepath: string) => boolean;
18
+ declare const isTextFile: (filepath: string, length?: number) => Promise<boolean>;
19
+ declare const isValidFile: (filepath: string) => Promise<boolean>;
20
+ declare const isValidPath: (filepath: string) => Promise<boolean>;
21
+
22
+ declare const getRecordTypes: (filepath: string) => Promise<(RecordFtype | RecordType)[]>;
23
+
24
+ declare function getVideoStandard(width: number, height: number): {
25
+ standard: StandardItem;
26
+ adapts: StandardItem[];
27
+ };
28
+ declare const getStandardInfo: (width: number, height: number) => StandardItem;
29
+ type StandardKey = "h_p360" | "h_p540" | "h_p720" | "h_p1080" | "h_p1440" | "h_k4" | "h_k8" | "h_k16" | "h_k32" | "h_k32plus" | "v_p360" | "v_p540" | "v_p720" | "v_p1080" | "v_p1440" | "v_k4" | "v_k8" | "v_k16" | "v_k32" | "v_k32plus";
30
+ type StandardItem = {
31
+ key: StandardKey;
32
+ is_horizontal: boolean;
33
+ width: number;
34
+ height: number;
35
+ label: string;
36
+ tag: string;
37
+ edge: "width" | "height";
38
+ codec: string;
39
+ };
40
+
41
+ export { type StandardItem, type StandardKey, getFileSize, getFileinfo, getRecordTypes, getStandardInfo, getVideoStandard, isAudioAdapt, isAudioHigh, isSubtitle, isTextFile, isValidFile, isValidPath, isVideoSupport };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var e,t=Object.create,i=Object.defineProperty,a=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,r=Object.getPrototypeOf,h=Object.prototype.hasOwnProperty,d=(e,t,r,d)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let c of o(t))h.call(e,c)||c===r||i(e,c,{get:()=>t[c],enumerable:!(d=a(t,c))||d.enumerable});return e},c={};((e,t)=>{for(var a in t)i(e,a,{get:t[a],enumerable:!0})})(c,{getFileSize:()=>v,getFileinfo:()=>E,getRecordTypes:()=>m,getStandardInfo:()=>N,getVideoStandard:()=>C,isAudioAdapt:()=>_,isAudioHigh:()=>b,isSubtitle:()=>f,isTextFile:()=>u,isValidFile:()=>w,isValidPath:()=>O,isVideoSupport:()=>y}),module.exports=(e=c,d(i({},"__esModule",{value:!0}),e));var l=require("@soga/types"),s=((e,a,o)=>(o=null!=e?t(r(e)):{},d(!a&&e&&e.__esModule?o:i(o,"default",{value:e,enumerable:!0}),e)))(require("mime")),n=require("path"),p=require("fs/promises"),g=require("fs-extra"),y=e=>["avc","h264","avc1","hevc","h265","vp9","av1"].includes(e),_=e=>["aac","mp3"].includes(e),b=e=>["flac"].includes(e),f=e=>{const t=(0,n.parse)(e).ext;return!![".srt",".vtt",".ass",".ssa",".sbv",".lrc"].includes(t)},u=async(e,t=5120)=>{const i=await(0,p.open)(e,"r");for(let e=0;e<t;e++){const t=Buffer.alloc(1),{bytesRead:a}=await i.read(t,0,1,e),o=t.toString().charCodeAt(0);if(0===a)return await i.close(),!0;if(1===a&&0===o)return await i.close(),!1}return await i.close(),!0},w=async e=>{if(!await(0,g.pathExists)(e))return!1;return await v(e)>0},O=async e=>!!await(0,g.pathExists)(e),R=require("@soga/types"),m=async e=>{const t=s.default.getType(e);if(!t){return await u(e)?[R.RecordType.DOC,R.RecordFtype.NONE]:[R.RecordType.OTHER,R.RecordFtype.NONE]}return t.startsWith("text/")&&e.endsWith(".txt")?await u(e)?[R.RecordType.DOC,R.RecordFtype.DOC_TXT]:[R.RecordType.OTHER,R.RecordFtype.NONE]:t.startsWith("video/")?e.endsWith(".ts")&&await u(e)?[R.RecordType.DOC,x(t,e)]:[R.RecordType.VIDEO,R.RecordFtype.VIDEO_NORMAL]:t.startsWith("audio/")?[R.RecordType.AUDIO,R.RecordFtype.NONE]:t.startsWith("image/")?(console.log("image mimetype",t),"image/heic"==t||"image/heif"==t?[R.RecordType.IMAGE,R.RecordFtype.IMAGE_HEIC]:[R.RecordType.IMAGE,R.RecordFtype.IMAGE_NORMAL]):t.startsWith("application/vnd.rn-realmedia")?[R.RecordType.VIDEO,R.RecordFtype.VIDEO_NORMAL]:t.startsWith("application/pdf")?[R.RecordType.DOC,R.RecordFtype.DOC_PDF]:t.startsWith("application/vnd.openxmlformats-officedocument.presentationml")?[R.RecordType.DOC,R.RecordFtype.DOC_PPT]:t.startsWith("application/vnd.openxmlformats-officedocument.wordprocessingml")?[R.RecordType.DOC,R.RecordFtype.DOC_WORD]:t.startsWith("application/vnd.openxmlformats-officedocument.spreadsheetml")?[R.RecordType.DOC,R.RecordFtype.DOC_EXCEL]:await u(e)?[R.RecordType.DOC,x(t,e)]:[R.RecordType.OTHER,R.RecordFtype.NONE]};function x(e,t){if(e.startsWith("text/markdown"))return R.RecordFtype.DOC_MD;const i=t.split(".").pop()?.toLowerCase();if(!i)return R.RecordFtype.DOC_NORMAL;return["js","ts","jsx","tsx","css","scss","less","py","java","c","cpp","cs","go","php","rb","swift","kt","rs","kotlin","dart","lua","perl","html","xml","json","yaml","yml","csv","log","sql","conf","bat"].includes(i)?R.RecordFtype.DOC_CODE:R.RecordFtype.DOC_NORMAL}var k=require("fs-extra"),D=require("path"),v=async e=>(await(0,k.stat)(e)).size,E=async e=>{try{const t=(0,D.basename)(e),i=await(0,k.stat)(e),a=i.isDirectory(),[o,r]=a?[l.RecordType.FOLDER,l.RecordFtype.NONE]:await m(e);return{filename:t,local_ctime:Math.floor(i.ctimeMs),local_mtime:Math.floor(i.mtimeMs),local_btime:Math.floor(i.birthtimeMs),record_type:o,record_ftype:r,size:a?0:i.size}}catch(t){const i=Date.now();return{filename:(0,D.basename)(e),local_ctime:i,local_mtime:i,local_btime:i,record_type:l.RecordType.OTHER,record_ftype:l.RecordFtype.NONE,size:0}}},T=new Map,z=[{key:"h_p360",is_horizontal:!0,width:640,height:360,label:"360p",tag:"",edge:"width",codec:"libx264"},{key:"h_p540",is_horizontal:!0,width:960,height:540,label:"540p",tag:"",edge:"width",codec:"libx264"},{key:"h_p720",is_horizontal:!0,width:1280,height:720,label:"720p",tag:"",edge:"width",codec:"libx264"},{key:"h_p1080",is_horizontal:!0,width:1920,height:1080,label:"1080p",tag:"HD",edge:"width",codec:"libx264"},{key:"h_p1440",is_horizontal:!0,width:2560,height:1440,label:"1440p",tag:"HD",edge:"width",codec:"libx265"},{key:"h_k4",is_horizontal:!0,width:3840,height:2160,label:"2160p",tag:"4K",edge:"width",codec:"libx265"},{key:"h_k8",is_horizontal:!0,width:7680,height:4320,label:"4320p",tag:"8K",edge:"width",codec:"libx265"},{key:"h_k16",is_horizontal:!0,width:15360,height:8640,label:"16K",tag:"",edge:"width",codec:"libx265"},{key:"h_k32",is_horizontal:!0,width:30720,height:17280,label:"32K",tag:"32K",edge:"width",codec:"libx265"},{key:"h_k32plus",is_horizontal:!0,width:61440,height:34560,label:"64K",tag:"",edge:"width",codec:"libx265"}],F=[{key:"v_p360",is_horizontal:!1,height:640,width:360,label:"360p",tag:"",edge:"height",codec:"libx264"},{key:"v_p540",is_horizontal:!1,height:960,width:540,label:"540p",tag:"",edge:"height",codec:"libx264"},{key:"v_p720",is_horizontal:!1,height:1280,width:720,label:"720p",tag:"",edge:"height",codec:"libx264"},{key:"v_p1080",is_horizontal:!1,height:1920,width:1080,label:"1080p",tag:"HD",edge:"height",codec:"libx264"},{key:"v_p1440",is_horizontal:!1,height:2560,width:1440,label:"1440p",tag:"HD",edge:"height",codec:"libx265"},{key:"v_k4",is_horizontal:!1,height:3840,width:2160,label:"2160p",tag:"4K",edge:"height",codec:"libx265"},{key:"v_k8",is_horizontal:!1,height:7680,width:4320,label:"4320p",tag:"8K",edge:"height",codec:"libx265"},{key:"v_k16",is_horizontal:!1,height:15360,width:8640,label:"16K",tag:"",edge:"height",codec:"libx265"},{key:"v_k32",is_horizontal:!1,height:30720,width:17280,label:"32K",tag:"32K",edge:"height",codec:"libx265"},{key:"v_k32plus",is_horizontal:!1,height:61440,width:34560,label:"64K",tag:"",edge:"height",codec:"libx265"}];function C(e,t){if(e<t){const i=[],a=[],{length:o}=F;for(let r=0;r<o;r++){const o=F[r],h=t>=.9*o.height,d=e>=.9*o.width;if(h&&i.unshift({...o,edge:"height"}),d&&a.unshift({...o,edge:"width"}),!h&&!d)break}const r=i.shift()??F[0],h=a.shift()??F[0],d=r.height>h.height;return{standard:d?r:h,adapts:d?i:a}}{const i=[],a=[],{length:o}=z;for(let r=0;r<o;r++){const o=z[r],h=e>=.9*o.width,d=t>=.9*o.height;if(h&&i.unshift({...o,edge:"width"}),d&&a.unshift({...o,edge:"height"}),!h&&!d)break}const r=i.shift()??z[0],h=a.shift()??z[0],d=r.width>h.width;return{standard:d?r:h,adapts:d?i:a}}}z.forEach(e=>{T.set(`horizontal_${e.width}`,e)}),F.forEach(e=>{T.set(`vertical_${e.height}`,e)});var N=(e,t)=>{const{standard:i}=C(e,t);return i};
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{RecordFtype as t,RecordType as e}from"@soga/types";import i from"mime";import{parse as a}from"path";import{open as h}from"fs/promises";import{pathExists as o}from"fs-extra";var s=t=>["avc","h264","avc1","hevc","h265","vp9","av1"].includes(t),l=t=>["aac","mp3"].includes(t),r=t=>["flac"].includes(t),c=t=>{const e=a(t).ext;return!![".srt",".vtt",".ass",".ssa",".sbv",".lrc"].includes(e)},d=async(t,e=5120)=>{const i=await h(t,"r");for(let t=0;t<e;t++){const e=Buffer.alloc(1),{bytesRead:a}=await i.read(e,0,1,t),h=e.toString().charCodeAt(0);if(0===a)return await i.close(),!0;if(1===a&&0===h)return await i.close(),!1}return await i.close(),!0},n=async t=>{if(!await o(t))return!1;return await y(t)>0},g=async t=>!!await o(t);import{RecordFtype as p,RecordType as _}from"@soga/types";var f=async t=>{const e=i.getType(t);if(!e){return await d(t)?[_.DOC,p.NONE]:[_.OTHER,p.NONE]}return e.startsWith("text/")&&t.endsWith(".txt")?await d(t)?[_.DOC,p.DOC_TXT]:[_.OTHER,p.NONE]:e.startsWith("video/")?t.endsWith(".ts")&&await d(t)?[_.DOC,m(e,t)]:[_.VIDEO,p.VIDEO_NORMAL]:e.startsWith("audio/")?[_.AUDIO,p.NONE]:e.startsWith("image/")?(console.log("image mimetype",e),"image/heic"==e||"image/heif"==e?[_.IMAGE,p.IMAGE_HEIC]:[_.IMAGE,p.IMAGE_NORMAL]):e.startsWith("application/vnd.rn-realmedia")?[_.VIDEO,p.VIDEO_NORMAL]:e.startsWith("application/pdf")?[_.DOC,p.DOC_PDF]:e.startsWith("application/vnd.openxmlformats-officedocument.presentationml")?[_.DOC,p.DOC_PPT]:e.startsWith("application/vnd.openxmlformats-officedocument.wordprocessingml")?[_.DOC,p.DOC_WORD]:e.startsWith("application/vnd.openxmlformats-officedocument.spreadsheetml")?[_.DOC,p.DOC_EXCEL]:await d(t)?[_.DOC,m(e,t)]:[_.OTHER,p.NONE]};function m(t,e){if(t.startsWith("text/markdown"))return p.DOC_MD;const i=e.split(".").pop()?.toLowerCase();if(!i)return p.DOC_NORMAL;return["js","ts","jsx","tsx","css","scss","less","py","java","c","cpp","cs","go","php","rb","swift","kt","rs","kotlin","dart","lua","perl","html","xml","json","yaml","yml","csv","log","sql","conf","bat"].includes(i)?p.DOC_CODE:p.DOC_NORMAL}import{stat as w}from"fs-extra";import{basename as b}from"path";var y=async t=>(await w(t)).size,O=async i=>{try{const a=b(i),h=await w(i),o=h.isDirectory(),[s,l]=o?[e.FOLDER,t.NONE]:await f(i);return{filename:a,local_ctime:Math.floor(h.ctimeMs),local_mtime:Math.floor(h.mtimeMs),local_btime:Math.floor(h.birthtimeMs),record_type:s,record_ftype:l,size:o?0:h.size}}catch(a){const h=Date.now();return{filename:b(i),local_ctime:h,local_mtime:h,local_btime:h,record_type:e.OTHER,record_ftype:t.NONE,size:0}}},k=new Map,u=[{key:"h_p360",is_horizontal:!0,width:640,height:360,label:"360p",tag:"",edge:"width",codec:"libx264"},{key:"h_p540",is_horizontal:!0,width:960,height:540,label:"540p",tag:"",edge:"width",codec:"libx264"},{key:"h_p720",is_horizontal:!0,width:1280,height:720,label:"720p",tag:"",edge:"width",codec:"libx264"},{key:"h_p1080",is_horizontal:!0,width:1920,height:1080,label:"1080p",tag:"HD",edge:"width",codec:"libx264"},{key:"h_p1440",is_horizontal:!0,width:2560,height:1440,label:"1440p",tag:"HD",edge:"width",codec:"libx265"},{key:"h_k4",is_horizontal:!0,width:3840,height:2160,label:"2160p",tag:"4K",edge:"width",codec:"libx265"},{key:"h_k8",is_horizontal:!0,width:7680,height:4320,label:"4320p",tag:"8K",edge:"width",codec:"libx265"},{key:"h_k16",is_horizontal:!0,width:15360,height:8640,label:"16K",tag:"",edge:"width",codec:"libx265"},{key:"h_k32",is_horizontal:!0,width:30720,height:17280,label:"32K",tag:"32K",edge:"width",codec:"libx265"},{key:"h_k32plus",is_horizontal:!0,width:61440,height:34560,label:"64K",tag:"",edge:"width",codec:"libx265"}],x=[{key:"v_p360",is_horizontal:!1,height:640,width:360,label:"360p",tag:"",edge:"height",codec:"libx264"},{key:"v_p540",is_horizontal:!1,height:960,width:540,label:"540p",tag:"",edge:"height",codec:"libx264"},{key:"v_p720",is_horizontal:!1,height:1280,width:720,label:"720p",tag:"",edge:"height",codec:"libx264"},{key:"v_p1080",is_horizontal:!1,height:1920,width:1080,label:"1080p",tag:"HD",edge:"height",codec:"libx264"},{key:"v_p1440",is_horizontal:!1,height:2560,width:1440,label:"1440p",tag:"HD",edge:"height",codec:"libx265"},{key:"v_k4",is_horizontal:!1,height:3840,width:2160,label:"2160p",tag:"4K",edge:"height",codec:"libx265"},{key:"v_k8",is_horizontal:!1,height:7680,width:4320,label:"4320p",tag:"8K",edge:"height",codec:"libx265"},{key:"v_k16",is_horizontal:!1,height:15360,width:8640,label:"16K",tag:"",edge:"height",codec:"libx265"},{key:"v_k32",is_horizontal:!1,height:30720,width:17280,label:"32K",tag:"32K",edge:"height",codec:"libx265"},{key:"v_k32plus",is_horizontal:!1,height:61440,width:34560,label:"64K",tag:"",edge:"height",codec:"libx265"}];function D(t,e){if(t<e){const i=[],a=[],{length:h}=x;for(let o=0;o<h;o++){const h=x[o],s=e>=.9*h.height,l=t>=.9*h.width;if(s&&i.unshift({...h,edge:"height"}),l&&a.unshift({...h,edge:"width"}),!s&&!l)break}const o=i.shift()??x[0],s=a.shift()??x[0],l=o.height>s.height;return{standard:l?o:s,adapts:l?i:a}}{const i=[],a=[],{length:h}=u;for(let o=0;o<h;o++){const h=u[o],s=t>=.9*h.width,l=e>=.9*h.height;if(s&&i.unshift({...h,edge:"width"}),l&&a.unshift({...h,edge:"height"}),!s&&!l)break}const o=i.shift()??u[0],s=a.shift()??u[0],l=o.width>s.width;return{standard:l?o:s,adapts:l?i:a}}}u.forEach(t=>{k.set(`horizontal_${t.width}`,t)}),x.forEach(t=>{k.set(`vertical_${t.height}`,t)});var v=(t,e)=>{const{standard:i}=D(t,e);return i};export{y as getFileSize,O as getFileinfo,f as getRecordTypes,v as getStandardInfo,D as getVideoStandard,l as isAudioAdapt,r as isAudioHigh,c as isSubtitle,d as isTextFile,n as isValidFile,g as isValidPath,s as isVideoSupport};
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@soga/fileutils",
3
+ "version": "1.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts",
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "scripts": {
15
+ "build": "rimraf dist && tsup src/index.ts --format cjs,esm --dts --minify terser",
16
+ "prepublishOnly": "npm run build"
17
+ },
18
+ "devDependencies": {
19
+ "@types/node": "^24.5.2",
20
+ "@types/fs-extra": "^11.0.4",
21
+ "@soga/typescript-config": "latest",
22
+ "rimraf": "^6.0.1",
23
+ "terser": "^5.43.1",
24
+ "tsup": "^8.5.0",
25
+ "typescript": "^5.8.3"
26
+ },
27
+ "keywords": [],
28
+ "author": "",
29
+ "license": "ISC",
30
+ "dependencies": {
31
+ "@soga/types": "latest",
32
+ "mime": "^4.0.7",
33
+ "fs-extra": "^11.3.2"
34
+ }
35
+ }