initial version
This commit is contained in:
67
src/lib/files.ts
Normal file
67
src/lib/files.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import type { DirectoryListing, FileEntry, MediaType } from '@/types'
|
||||
|
||||
const HIDDEN_FILES = /^\./
|
||||
|
||||
const VIDEO_EXTENSIONS = new Set(['.mp4', '.mov', '.mkv', '.avi', '.webm', '.m4v'])
|
||||
const IMAGE_EXTENSIONS = new Set(['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.tiff', '.tif'])
|
||||
|
||||
function getMediaType(filename: string): MediaType {
|
||||
const ext = path.extname(filename).toLowerCase()
|
||||
if (VIDEO_EXTENSIONS.has(ext)) return 'video'
|
||||
if (IMAGE_EXTENSIONS.has(ext)) return 'image'
|
||||
return 'other'
|
||||
}
|
||||
|
||||
function fileApiUrl(libraryId: string, relativePath: string): string {
|
||||
return `/api/file?libraryId=${encodeURIComponent(libraryId)}&path=${encodeURIComponent(relativePath)}`
|
||||
}
|
||||
|
||||
export function scanDirectory(
|
||||
libraryRoot: string,
|
||||
libraryId: string,
|
||||
subpath: string
|
||||
): DirectoryListing {
|
||||
const absPath = subpath
|
||||
? path.resolve(libraryRoot, subpath)
|
||||
: libraryRoot
|
||||
|
||||
let dirents: fs.Dirent[]
|
||||
try {
|
||||
dirents = fs.readdirSync(absPath, { withFileTypes: true })
|
||||
} catch {
|
||||
return { path: subpath, entries: [] }
|
||||
}
|
||||
|
||||
const entries: FileEntry[] = dirents
|
||||
.filter((d) => !HIDDEN_FILES.test(d.name))
|
||||
.map((d): FileEntry => {
|
||||
if (d.isDirectory()) {
|
||||
return {
|
||||
name: d.name,
|
||||
type: 'directory',
|
||||
mediaType: null,
|
||||
url: null,
|
||||
}
|
||||
}
|
||||
|
||||
const relPath = subpath ? path.join(subpath, d.name) : d.name
|
||||
const mediaType = getMediaType(d.name)
|
||||
|
||||
return {
|
||||
name: d.name,
|
||||
type: 'file',
|
||||
mediaType,
|
||||
url: fileApiUrl(libraryId, relPath),
|
||||
}
|
||||
})
|
||||
|
||||
// Sort: directories first, then files; each group sorted alphabetically
|
||||
entries.sort((a, b) => {
|
||||
if (a.type !== b.type) return a.type === 'directory' ? -1 : 1
|
||||
return a.name.localeCompare(b.name)
|
||||
})
|
||||
|
||||
return { path: subpath, entries }
|
||||
}
|
||||
Reference in New Issue
Block a user