首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uint `json:"id" gorm:"primaryKey;comment:用户ID"`
|
||||
UserCode string `json:"user_code" gorm:"uniqueIndex;size:8;comment:用户码(8位随机字母数字)"`
|
||||
Username string `json:"username" gorm:"uniqueIndex;size:50;comment:用户名"`
|
||||
Password string `json:"-" gorm:"size:255;comment:密码(加密存储)"`
|
||||
Email string `json:"email" gorm:"size:100;comment:邮箱"`
|
||||
Phone string `json:"phone" gorm:"size:20;comment:手机号"`
|
||||
Nickname string `json:"nickname" gorm:"size:50;comment:昵称"`
|
||||
StorageUsed int64 `json:"storage_used" gorm:"default:0;comment:已使用存储空间(字节)"`
|
||||
StorageLimit int64 `json:"storage_limit" gorm:"default:52428800;comment:存储空间限制(字节),默认50MB"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"comment:创建时间"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"comment:更新时间"`
|
||||
DeletedAt gorm.DeletedAt `json:"-" gorm:"comment:删除时间"`
|
||||
}
|
||||
|
||||
func (User) TableName() string {
|
||||
return "users"
|
||||
}
|
||||
|
||||
func (User) TableComment() string {
|
||||
return "用户表"
|
||||
}
|
||||
|
||||
func NewUser(username, password, userCode, nickname string) *User {
|
||||
if nickname == "" {
|
||||
nickname = username
|
||||
}
|
||||
return &User{
|
||||
Username: username,
|
||||
Password: password,
|
||||
UserCode: userCode,
|
||||
Nickname: nickname,
|
||||
StorageUsed: 0,
|
||||
StorageLimit: 52428800,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package repository
|
||||
|
||||
import "cloudnest/internal/domain/auth/entity"
|
||||
|
||||
// UserRepository defines the interface for user data access operations.
|
||||
// UserRepository 定义了用户数据访问操作的接口。
|
||||
//
|
||||
// This interface follows the Dependency Inversion Principle (DIP),
|
||||
// where the application layer depends on this abstraction rather than concrete implementations.
|
||||
//
|
||||
// 此接口遵循依赖倒置原则(DIP),应用层依赖于这个抽象而非具体实现。
|
||||
type UserRepository interface {
|
||||
FindByUsername(username string) (*entity.User, error)
|
||||
FindByUserCode(userCode string) (*entity.User, error)
|
||||
FindByEmail(email string) (*entity.User, error)
|
||||
Create(user *entity.User) error
|
||||
FindByID(id uint) (*entity.User, error)
|
||||
UpdateStorageUsed(userCode string, size int64) error
|
||||
UpdateProfile(userCode, email, phone, nickname string) error
|
||||
UpdatePassword(userCode, newPassword string) error
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type DeletedFile struct {
|
||||
ID uint `json:"id" gorm:"primaryKey;comment:删除记录ID"`
|
||||
UserID uint `json:"user_id" gorm:"comment:用户ID"`
|
||||
UserCode string `json:"user_code" gorm:"index;size:8;comment:用户码"`
|
||||
FileKey string `json:"file_key" gorm:"size:500;comment:文件存储键"`
|
||||
FileName string `json:"file_name" gorm:"size:255;comment:文件名"`
|
||||
Size int64 `json:"size" gorm:"comment:文件大小(字节)"`
|
||||
DeletedAt time.Time `json:"deleted_at" gorm:"comment:删除时间"`
|
||||
RemovedAt gorm.DeletedAt `json:"-" gorm:"comment:物理删除时间"`
|
||||
}
|
||||
|
||||
func (DeletedFile) TableName() string {
|
||||
return "deleted_files"
|
||||
}
|
||||
|
||||
func (DeletedFile) TableComment() string {
|
||||
return "回收站文件表"
|
||||
}
|
||||
|
||||
func NewDeletedFile(userID uint, userCode, fileKey, fileName string, size int64) *DeletedFile {
|
||||
return &DeletedFile{
|
||||
UserID: userID,
|
||||
UserCode: userCode,
|
||||
FileKey: fileKey,
|
||||
FileName: fileName,
|
||||
Size: size,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Favorite struct {
|
||||
ID uint `json:"id" gorm:"primaryKey;comment:收藏ID"`
|
||||
UserID uint `json:"user_id" gorm:"comment:用户ID"`
|
||||
UserCode string `json:"user_code" gorm:"index;size:8;comment:用户码"`
|
||||
FileKey string `json:"file_key" gorm:"size:500;comment:文件存储键"`
|
||||
Size int64 `json:"size" gorm:"default:0;comment:文件大小(字节)"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"comment:收藏时间"`
|
||||
DeletedAt gorm.DeletedAt `json:"-" gorm:"comment:删除时间"`
|
||||
}
|
||||
|
||||
func (Favorite) TableName() string {
|
||||
return "favorites"
|
||||
}
|
||||
|
||||
func (Favorite) TableComment() string {
|
||||
return "用户收藏表"
|
||||
}
|
||||
|
||||
func NewFavorite(userID uint, userCode, fileKey string, size int64) *Favorite {
|
||||
return &Favorite{
|
||||
UserID: userID,
|
||||
UserCode: userCode,
|
||||
FileKey: fileKey,
|
||||
Size: size,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package entity
|
||||
|
||||
// FileMeta contains metadata about a file stored in the object storage.
|
||||
// FileMeta 包含存储在对象存储中的文件元数据。
|
||||
//
|
||||
// Fields:
|
||||
// - Key: The unique key/path of the file in the storage.
|
||||
// - Size: The size of the file in bytes.
|
||||
//
|
||||
// 字段:
|
||||
// - Key: 文件在存储中的唯一键/路径。
|
||||
// - Size: 文件大小(字节)。
|
||||
type FileMeta struct {
|
||||
Key string `json:"key"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
// UploadRequest contains the necessary information to upload a file.
|
||||
// UploadRequest 包含上传文件所需的必要信息。
|
||||
//
|
||||
// Fields:
|
||||
// - Filename: The original filename.
|
||||
// - Content: The file content as bytes.
|
||||
// - ContentType: The MIME type of the file.
|
||||
//
|
||||
// 字段:
|
||||
// - Filename: 原始文件名。
|
||||
// - Content: 文件内容(字节)。
|
||||
// - ContentType: 文件的 MIME 类型。
|
||||
type UploadRequest struct {
|
||||
Filename string
|
||||
Content []byte
|
||||
ContentType string
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package repository
|
||||
|
||||
import "io"
|
||||
|
||||
// FileInfo contains metadata for a file.
|
||||
// FileInfo 包含文件的元数据。
|
||||
//
|
||||
// Fields:
|
||||
// - Key: The storage key/path of the file.
|
||||
// - Size: The file size in bytes.
|
||||
// - LastModified: The timestamp when the file was last modified.
|
||||
//
|
||||
// 字段:
|
||||
// - Key: 文件的存储键/路径。
|
||||
// - Size: 文件大小(字节)。
|
||||
// - LastModified: 文件最后修改的时间戳。
|
||||
type FileInfo struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified string
|
||||
}
|
||||
|
||||
// FileRepository defines the interface for file storage operations.
|
||||
// FileRepository 定义了文件存储操作的接口。
|
||||
//
|
||||
// This interface follows the Dependency Inversion Principle (DIP),
|
||||
// where the application layer depends on this abstraction rather than concrete implementations.
|
||||
//
|
||||
// 此接口遵循依赖倒置原则(DIP),应用层依赖于这个抽象而非具体实现。
|
||||
type FileRepository interface {
|
||||
// Upload uploads a file to the object storage.
|
||||
// Upload 将文件上传到对象存储。
|
||||
//
|
||||
// Parameters:
|
||||
// - bucket: The target bucket name.
|
||||
// - objectName: The unique name/path for the object.
|
||||
// - reader: The file content reader.
|
||||
// - size: The size of the file in bytes.
|
||||
// - contentType: The MIME type of the file.
|
||||
//
|
||||
// Returns:
|
||||
// - error: An error if the operation fails.
|
||||
//
|
||||
// 参数:
|
||||
// - bucket: 目标桶名称。
|
||||
// - objectName: 对象的唯一名称/路径。
|
||||
// - reader: 文件内容读取器。
|
||||
// - size: 文件大小(字节)。
|
||||
// - contentType: 文件的 MIME 类型。
|
||||
//
|
||||
// 返回值:
|
||||
// - error: 如果操作失败则返回错误。
|
||||
Upload(bucket, objectName string, reader io.Reader, size int64, contentType string) error
|
||||
|
||||
// List retrieves a list of files under the specified prefix with metadata.
|
||||
// List 获取指定前缀下的文件列表及其元数据。
|
||||
//
|
||||
// Parameters:
|
||||
// - bucket: The bucket name.
|
||||
// - prefix: The prefix/path to filter files.
|
||||
//
|
||||
// Returns:
|
||||
// - []FileInfo: A list of file information including key, size, and last modified time.
|
||||
// - error: An error if the operation fails.
|
||||
//
|
||||
// 参数:
|
||||
// - bucket: 桶名称。
|
||||
// - prefix: 用于过滤文件的前缀/路径。
|
||||
//
|
||||
// 返回值:
|
||||
// - []FileInfo: 文件信息列表,包含键、大小和最后修改时间。
|
||||
// - error: 如果操作失败则返回错误。
|
||||
List(bucket, prefix string) ([]FileInfo, error)
|
||||
|
||||
// PresignDownload generates a pre-signed URL for downloading a file.
|
||||
// PresignDownload 生成用于下载文件的预签名 URL。
|
||||
//
|
||||
// Parameters:
|
||||
// - bucket: The bucket name.
|
||||
// - objectName: The name/path of the object.
|
||||
//
|
||||
// Returns:
|
||||
// - string: The pre-signed download URL.
|
||||
// - error: An error if the operation fails.
|
||||
//
|
||||
// Note:
|
||||
// - The URL is time-limited and doesn't require authentication.
|
||||
//
|
||||
// 参数:
|
||||
// - bucket: 桶名称。
|
||||
// - objectName: 对象的名称/路径。
|
||||
//
|
||||
// 返回值:
|
||||
// - string: 预签名下载 URL。
|
||||
// - error: 如果操作失败则返回错误。
|
||||
//
|
||||
// 注意:
|
||||
// - URL 有时间限制且不需要认证。
|
||||
PresignDownload(bucket, objectName string) (string, error)
|
||||
|
||||
// Delete removes a file from the object storage.
|
||||
// Delete 从对象存储中删除文件。
|
||||
//
|
||||
// Parameters:
|
||||
// - bucket: The bucket name.
|
||||
// - objectName: The name/path of the object to delete.
|
||||
//
|
||||
// Returns:
|
||||
// - error: An error if the operation fails.
|
||||
//
|
||||
// 参数:
|
||||
// - bucket: 桶名称。
|
||||
// - objectName: 要删除的对象的名称/路径。
|
||||
//
|
||||
// 返回值:
|
||||
// - error: 如果操作失败则返回错误。
|
||||
Delete(bucket, objectName string) error
|
||||
|
||||
// GetObject retrieves a file as a readable stream from the object storage.
|
||||
// GetObject 从对象存储中获取文件的可读流。
|
||||
//
|
||||
// Parameters:
|
||||
// - bucket: The bucket name.
|
||||
// - objectName: The name/path of the object.
|
||||
//
|
||||
// Returns:
|
||||
// - io.ReadCloser: A readable stream of the file content. Caller must close it.
|
||||
// - int64: The file size in bytes.
|
||||
// - string: The Content-Type of the file.
|
||||
// - error: An error if the operation fails.
|
||||
//
|
||||
// 参数:
|
||||
// - bucket: 桶名称。
|
||||
// - objectName: 对象的名称/路径。
|
||||
//
|
||||
// 返回值:
|
||||
// - io.ReadCloser: 文件内容的可读流。调用方必须关闭它。
|
||||
// - int64: 文件大小(字节)。
|
||||
// - string: 文件的 Content-Type。
|
||||
// - error: 如果操作失败则返回错误。
|
||||
GetObject(bucket, objectName string) (io.ReadCloser, int64, string, error)
|
||||
|
||||
GetObjectInfo(bucket, objectName string) (int64, string, error)
|
||||
}
|
||||
Reference in New Issue
Block a user