Implement new file upload and download mechanism
This commit is contained in:
@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"github.com/fossyy/filekeeper/types/models"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"time"
|
||||
)
|
||||
@ -21,7 +22,7 @@ func NewService(db types.Database, cache types.CachingServer) *Service {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Service) GetUser(ctx context.Context, email string) (*types.UserWithExpired, error) {
|
||||
func (r *Service) GetUser(ctx context.Context, email string) (*models.User, error) {
|
||||
userJSON, err := app.Server.Cache.GetCache(ctx, "UserCache:"+email)
|
||||
if err == redis.Nil {
|
||||
userData, err := r.db.GetUser(email)
|
||||
@ -29,13 +30,12 @@ func (r *Service) GetUser(ctx context.Context, email string) (*types.UserWithExp
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user := &types.UserWithExpired{
|
||||
user := &models.User{
|
||||
UserID: userData.UserID,
|
||||
Username: userData.Username,
|
||||
Email: userData.Email,
|
||||
Password: userData.Password,
|
||||
Totp: userData.Totp,
|
||||
AccessAt: time.Now(),
|
||||
}
|
||||
|
||||
newUserJSON, _ := json.Marshal(user)
|
||||
@ -50,7 +50,7 @@ func (r *Service) GetUser(ctx context.Context, email string) (*types.UserWithExp
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var user types.UserWithExpired
|
||||
var user models.User
|
||||
err = json.Unmarshal([]byte(userJSON), &user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -66,7 +66,7 @@ func (r *Service) DeleteUser(email string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Service) GetFile(id string) (*types.FileWithExpired, error) {
|
||||
func (r *Service) GetFile(id string) (*models.File, error) {
|
||||
fileJSON, err := r.cache.GetCache(context.Background(), "FileCache:"+id)
|
||||
if err == redis.Nil {
|
||||
uploadData, err := r.db.GetFile(id)
|
||||
@ -74,30 +74,18 @@ func (r *Service) GetFile(id string) (*types.FileWithExpired, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileCache := &types.FileWithExpired{
|
||||
ID: uploadData.ID,
|
||||
OwnerID: uploadData.OwnerID,
|
||||
Name: uploadData.Name,
|
||||
Size: uploadData.Size,
|
||||
Downloaded: uploadData.Downloaded,
|
||||
UploadedByte: uploadData.UploadedByte,
|
||||
UploadedChunk: uploadData.UploadedChunk,
|
||||
Done: uploadData.Done,
|
||||
AccessAt: time.Now(),
|
||||
}
|
||||
|
||||
newFileJSON, _ := json.Marshal(fileCache)
|
||||
newFileJSON, _ := json.Marshal(uploadData)
|
||||
err = r.cache.SetCache(context.Background(), "FileCache:"+id, newFileJSON, time.Hour*24)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fileCache, nil
|
||||
return uploadData, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var fileCache types.FileWithExpired
|
||||
var fileCache models.File
|
||||
err = json.Unmarshal([]byte(fileJSON), &fileCache)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -105,16 +93,18 @@ func (r *Service) GetFile(id string) (*types.FileWithExpired, error) {
|
||||
return &fileCache, nil
|
||||
}
|
||||
|
||||
func (r *Service) GetUserFile(name, ownerID string) (*types.FileWithExpired, error) {
|
||||
func (r *Service) GetUserFile(name, ownerID string) (*types.FileWithDetail, error) {
|
||||
fileData, err := r.db.GetUserFile(name, ownerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
file, err := r.GetFile(fileData.ID.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
dada := &types.FileWithDetail{
|
||||
ID: fileData.ID,
|
||||
OwnerID: fileData.OwnerID,
|
||||
Name: fileData.Name,
|
||||
Size: fileData.Size,
|
||||
Downloaded: fileData.Downloaded,
|
||||
}
|
||||
|
||||
return file, nil
|
||||
return dada, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user