This commit is contained in:
2025-09-15 05:49:14 +02:00
parent b47c9d1761
commit 5a7a6f832d
2 changed files with 53 additions and 153 deletions

View File

@@ -29,15 +29,53 @@ pub fn (self FsDir) type_name() string {
}
pub fn (self FsDir) dump(mut e encoder.Encoder) ! {
e.add_string(self.name)
e.add_u32(self.fs_id)
e.add_u32(self.parent_id)
// Handle directories array
e.add_u16(u16(self.directories.len))
for dir_id in self.directories {
e.add_u32(dir_id)
}
// Handle files array
e.add_u16(u16(self.files.len))
for file_id in self.files {
e.add_u32(file_id)
}
// Handle symlinks array
e.add_u16(u16(self.symlinks.len))
for symlink_id in self.symlinks {
e.add_u32(symlink_id)
}
}
fn (mut self DBFsDir) load(mut o FsDir, mut e encoder.Decoder) ! {
o.name = e.get_string()!
o.fs_id = e.get_u32()!
o.parent_id = e.get_u32()!
// Load directories array
directories_count := e.get_u16()!
o.directories = []u32{cap: int(directories_count)}
for _ in 0 .. directories_count {
o.directories << e.get_u32()!
}
// Load files array
files_count := e.get_u16()!
o.files = []u32{cap: int(files_count)}
for _ in 0 .. files_count {
o.files << e.get_u32()!
}
// Load symlinks array
symlinks_count := e.get_u16()!
o.symlinks = []u32{cap: int(symlinks_count)}
for _ in 0 .. symlinks_count {
o.symlinks << e.get_u32()!
}
}
@[params]
@@ -49,21 +87,28 @@ pub mut:
parent_id u32
tags []string
comments []db.CommentArg
directories []u32
files []u32
symlinks []u32
}
// get new directory, not from the DB
pub fn (mut self DBFsDir) new(args FsDirArg) !FsDir {
mut o := FsDir{
name: args.name
fs_id: args.fs_id
parent_id: args.parent_id
name: args.name
description: args.description
fs_id: args.fs_id
parent_id: args.parent_id
directories: args.directories
files: args.files
symlinks: args.symlinks
}
// Set base fields
o.description = args.description
o.tags = self.db.tags_get(args.tags)!
o.comments = self.db.comments_get(args.comments)!
o.updated_at = ourtime.now().unix()
o.created_at = ourtime.now().unix()
o.updated_at = o.created_at
return o
}
@@ -92,4 +137,4 @@ pub fn (mut self DBFsDir) get(id u32) !FsDir {
return o
}
// THERE IS NO LIST FUNCTION AS DIRECTORIES ARE ALWAYS KNOWN FROM THE FS, the FS points to the root directory by id
// THERE IS NO LIST FUNCTION AS DIRECTORIES ARE ALWAYS KNOWN FROM THE FS, the FS points to the root directory by id

View File

@@ -33,9 +33,7 @@ pub fn (self FsFile) type_name() string {
}
pub fn (self FsFile) dump(mut e encoder.Encoder) ! {
e.add_string(self.name)
e.add_u32(self.fs_id)
// Handle directories
e.add_u16(u16(self.directories.len))
for dir_id in self.directories {
@@ -62,7 +60,6 @@ pub fn (self FsFile) dump(mut e encoder.Encoder) ! {
}
fn (mut self DBFsFile) load(mut o FsFile, mut e encoder.Decoder) ! {
o.name = e.get_string()!
o.fs_id = e.get_u32()!
// Load directories
@@ -175,43 +172,12 @@ pub fn (mut self DBFsFile) set(o FsFile) !u32 {
id := self.db.set[FsFile](o)!
// Store file in each directory's file index
for dir_id in o.directories {
// Store by name in each directory
path_key := '${dir_id}:${o.name}'
self.db.redis.hset('fsfile:paths', path_key, id.str())!
// Add to directory's file list using hset
self.db.redis.hset('fsfile:dir:${dir_id}', id.str(), id.str())!
}
// Store in filesystem's file list using hset
self.db.redis.hset('fsfile:fs:${o.fs_id}', id.str(), id.str())!
// REMOVE: Store by mimetype using hset
return id
}
pub fn (mut self DBFsFile) delete(id u32) ! {
// Get the file info before deleting
file := self.get(id)!
// Remove from each directory's file index
for dir_id in file.directories {
// Remove from path index
path_key := '${dir_id}:${file.name}'
self.db.redis.hdel('fsfile:paths', path_key)!
// Remove from directory's file list using hdel
self.db.redis.hdel('fsfile:dir:${dir_id}', id.str())!
}
// Remove from filesystem's file list using hdel
self.db.redis.hdel('fsfile:fs:${file.fs_id}', id.str())!
// REMOVE: Remove from mimetype index using hdel
// Delete the file itself
self.db.delete[FsFile](id)!
}
@@ -227,74 +193,6 @@ pub fn (mut self DBFsFile) get(id u32) !FsFile {
return o
}
pub fn (mut self DBFsFile) list() ![]FsFile {
return self.db.list[FsFile]()!.map(self.get(it)!)
}
// Get file by path in a specific directory
pub fn (mut self DBFsFile) get_by_path(dir_id u32, name string) !FsFile {
path_key := '${dir_id}:${name}'
id_str := self.db.redis.hget('fsfile:paths', path_key)!
if id_str == '' {
return error('File "${name}" not found in directory ${dir_id}')
}
return self.get(id_str.u32())!
}
// List files in a directory
pub fn (mut self DBFsFile) list_by_directory(dir_id u32) ![]FsFile {
file_ids := self.db.redis.hkeys('fsfile:dir:${dir_id}')!
mut files := []FsFile{}
for id_str in file_ids {
files << self.get(id_str.u32())!
}
return files
}
// List files in a filesystem
pub fn (mut self DBFsFile) list_by_filesystem(fs_id u32) ![]FsFile {
file_ids := self.db.redis.hkeys('fsfile:fs:${fs_id}')!
mut files := []FsFile{}
for id_str in file_ids {
files << self.get(id_str.u32())!
}
return files
}
// Update file with a new blob (append)
pub fn (mut self DBFsFile) append_blob(id u32, blob_id u32) !u32 {
// Check blob exists
blob_exists := self.db.exists[FsBlob](blob_id)!
if !blob_exists {
return error('Blob with ID ${blob_id} does not exist')
}
// Get blob size
mut blob_obj, blob_data := self.db.get_data[FsBlob](blob_id)!
mut e_decoder := encoder.decoder_new(blob_data)
// Skip hash
e_decoder.get_string()!
// Skip data, get size directly
e_decoder.get_list_u8()!
blob_size := e_decoder.get_int()!
// Get file
mut file := self.get(id)!
// Add blob if not already in the list
if blob_id !in file.blobs {
file.blobs << blob_id
file.size_bytes += u64(blob_size)
file.updated_at = ourtime.now().unix()
}
// Save file
return self.set(file)!
}
// Update file accessed timestamp
pub fn (mut self DBFsFile) update_accessed(id u32) !u32 {
mut file := self.get(id)!
@@ -309,46 +207,3 @@ pub fn (mut self DBFsFile) update_metadata(id u32, key string, value string) !u3
file.updated_at = ourtime.now().unix()
return self.set(file)!
}
// Rename a file
pub fn (mut self DBFsFile) rename(id u32, new_name string) !u32 {
mut file := self.get(id)!
// Remove old path indexes
for dir_id in file.directories {
old_path_key := '${dir_id}:${file.name}'
self.db.redis.hdel('fsfile:paths', old_path_key)!
}
// Update name
file.name = new_name
// Save with new name
return self.set(file)!
}
// Move file to different directories
pub fn (mut self DBFsFile) move(id u32, new_directories []u32) !u32 {
mut file := self.get(id)!
// Check that all new directories exist
for dir_id in new_directories {
dir_exists := self.db.exists[FsDir](dir_id)!
if !dir_exists {
return error('Directory with ID ${dir_id} does not exist')
}
}
// Remove from old directories
for dir_id in file.directories {
path_key := '${dir_id}:${file.name}'
self.db.redis.hdel('fsfile:paths', path_key)!
self.db.redis.hdel('fsfile:dir:${dir_id}', id.str())!
}
// Update directories
file.directories = new_directories
// Save with new directories
return self.set(file)!
}