Add file management API with local storage backend

- Implement FileHandler with CRUD operations for files/directories
- Add FileService with business logic and SHA-256 hashing
- Create LocalStorage backend for filesystem persistence
- Add database repository with pagination and name uniqueness
  constraints
- Configure max upload size in storage settings
- Include comprehensive tests for all layers
This commit is contained in:
2026-06-24 14:08:57 +08:00
parent eaa31efd64
commit e2af482cc9
17 changed files with 1815 additions and 15 deletions
+30
View File
@@ -15,6 +15,8 @@ type FileRepository interface {
FindByID(ctx context.Context, id string) (*model.File, error)
FindByUserID(ctx context.Context, userID string, offset, limit int) ([]model.File, int64, error)
FindByParentID(ctx context.Context, userID string, parentID *string) ([]model.File, error)
FindByParentIDPaginated(ctx context.Context, userID string, parentID *string, offset, limit int) ([]model.File, int64, error)
FindByNameAndParent(ctx context.Context, userID string, parentID *string, name string) (*model.File, error)
Update(ctx context.Context, file *model.File) error
Delete(ctx context.Context, id string) error
}
@@ -76,6 +78,34 @@ func (r *fileRepository) FindByParentID(ctx context.Context, userID string, pare
return files, nil
}
func (r *fileRepository) FindByParentIDPaginated(ctx context.Context, userID string, parentID *string, offset, limit int) ([]model.File, int64, error) {
var files []model.File
var total int64
if err := r.db.WithContext(ctx).Model(&model.File{}).Where("user_id = ? AND parent_id IS ?", userID, parentID).Count(&total).Error; err != nil {
return nil, 0, err
}
result := r.db.WithContext(ctx).Where("user_id = ? AND parent_id IS ?", userID, parentID).Offset(offset).Limit(limit).Order("is_dir DESC, name ASC").Find(&files)
if result.Error != nil {
return nil, 0, result.Error
}
return files, total, nil
}
func (r *fileRepository) FindByNameAndParent(ctx context.Context, userID string, parentID *string, name string) (*model.File, error) {
var file model.File
result := r.db.WithContext(ctx).Where("user_id = ? AND parent_id IS ? AND name = ?", userID, parentID, name).First(&file)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, model.ErrNotFound
}
if result.Error != nil {
return nil, result.Error
}
return &file, nil
}
func (r *fileRepository) Update(ctx context.Context, file *model.File) error {
result := r.db.WithContext(ctx).Save(file)
if result.Error != nil {