-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Files and folders displaying & basic query operations
- Loading branch information
1 parent
c4d50fe
commit be71c5e
Showing
10 changed files
with
176 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,40 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
import { tokenStorageKey } from "@/lib/global"; | ||
|
||
export async function GET(req: NextRequest) { | ||
if(!req.cookies.get(tokenStorageKey)) return NextResponse.json({ status: 401 }); | ||
|
||
// const { searchParams } = new URL(req.url); | ||
// const targetPath = searchParams.get("path"); | ||
// // console.log(targetPath); | ||
|
||
// // return new NextResponse("ok"); | ||
const { searchParams } = new URL(req.url); | ||
const targetDisk = searchParams.get("disk"); | ||
const targetPath = path.join((targetDisk ?? "C") +":", searchParams.get("path") ?? "/"); | ||
|
||
// //@ts-ignore | ||
// if(!targetPath || !fs.existsSync(targetPath)) return NextResponse.json({ status: 404 }); | ||
try { | ||
if(!targetPath || !fs.existsSync(targetPath)) return NextResponse.json({ status: 404 }); | ||
|
||
// //@ts-ignore | ||
// const stat = fs.statSync(targetPath); | ||
|
||
// if(!stat.isDirectory()) return NextResponse.json({ status: 400 }); | ||
|
||
// return NextResponse.json({ | ||
// status: 200, | ||
// //@ts-ignore | ||
// items: fs.readdirSync(targetPath).map((itemName) => { | ||
// //@ts-ignore | ||
// const item = fs.statSync(path.join(targetPath, itemName)); | ||
|
||
// return { | ||
// name: itemName, | ||
// type: item.isDirectory() ? "folder" : "file", | ||
// size: item.size | ||
// }; | ||
// }) | ||
// }); | ||
|
||
return NextResponse.json({ | ||
status: 200, | ||
items: [ | ||
{ | ||
name: "test", | ||
type: "folder", | ||
size: 1024 | ||
} | ||
] | ||
}); | ||
const stat = fs.statSync(targetPath); | ||
|
||
if(!stat.isDirectory()) return NextResponse.json({ status: 400 }); | ||
|
||
return NextResponse.json({ | ||
status: 200, | ||
items: fs.readdirSync(targetPath).map((itemName) => { | ||
const item = fs.statSync(path.join(targetPath, itemName)); | ||
|
||
return { | ||
name: itemName, | ||
type: item.isDirectory() ? "folder" : "file", | ||
size: item.size | ||
}; | ||
}) | ||
}); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.log("[Server: /api/folder] "+ err); | ||
|
||
return NextResponse.json({ status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useEffect } from "react"; | ||
|
||
import Emitter from "@/lib/emitter"; | ||
|
||
type EmitterInstance = [string, (...args: any[]) => any]; | ||
|
||
/** | ||
* Create an event listener with `EventEmitter` | ||
* | ||
* @example | ||
* ```ts | ||
* useEmitter([ | ||
* ["foo", () => console.log("bar")] | ||
* ]); | ||
* | ||
* // in somewhere... | ||
* new Emitter().emit("foo"); // bar | ||
* ``` | ||
*/ | ||
export default function useEmitter(instances: EmitterInstance[]) { | ||
useEffect(() => { | ||
instances.forEach((instance: EmitterInstance) => { | ||
Emitter.get().on(instance[0], (...args: any[]) => instance[1](...args)); | ||
}); | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { EventEmitter } from "node:events"; | ||
|
||
export default class Emitter extends EventEmitter { | ||
private static instance: Emitter; | ||
|
||
private constructor() { | ||
super(); | ||
this.setMaxListeners(Infinity); | ||
} | ||
|
||
public static get(): Emitter { | ||
if(!this.instance) this.instance = new Emitter(); | ||
|
||
return this.instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { BytesType } from "@/types"; | ||
|
||
export function getCurrentState<T>(setState: React.Dispatch<React.SetStateAction<T>>): Promise<T> { | ||
return new Promise((resolve, _reject) => { | ||
setState((currentState) => { | ||
resolve(currentState); | ||
|
||
return currentState; | ||
}); | ||
}); | ||
} | ||
|
||
export function bytesSizeTransform(bytes: number, from: BytesType, to: BytesType, fixed: number = 2): { value: string, type: BytesType } { | ||
return { | ||
value: (bytes * Math.pow(1024, from - to)).toFixed(fixed), | ||
type: to | ||
}; | ||
} | ||
|
||
export function getBytesType(type: BytesType): string { | ||
switch(type) { | ||
case BytesType.B: return "B"; | ||
case BytesType.KB: return "KB"; | ||
case BytesType.MB: return "MB"; | ||
case BytesType.GB: return "GB"; | ||
case BytesType.TB: return "TB"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters