diff --git a/handler/file/file.go b/handler/file/file.go index 31d60bb..30df505 100644 --- a/handler/file/file.go +++ b/handler/file/file.go @@ -21,7 +21,7 @@ func GET(w http.ResponseWriter, r *http.Request) { var filesData []types.FileData for _, file := range files { - userFile, err := app.Server.Service.GetUserFile(r.Context(), file.Name, file.OwnerID.String()) + userFile, err := app.Server.Service.GetUserFile(r.Context(), file.ID) if err != nil { w.WriteHeader(http.StatusInternalServerError) app.Server.Logger.Error(err.Error()) diff --git a/handler/file/query/query.go b/handler/file/query/query.go index 4f9fac7..c896b2f 100644 --- a/handler/file/query/query.go +++ b/handler/file/query/query.go @@ -31,7 +31,7 @@ func GET(w http.ResponseWriter, r *http.Request) { var filesData []types.FileData for _, file := range files { - userFile, err := app.Server.Service.GetUserFile(r.Context(), file.Name, file.OwnerID.String()) + userFile, err := app.Server.Service.GetUserFile(r.Context(), file.ID) if err != nil { w.WriteHeader(http.StatusInternalServerError) app.Server.Logger.Error(err.Error()) diff --git a/handler/file/rename/rename.go b/handler/file/rename/rename.go index 4ff9d5c..6fac230 100644 --- a/handler/file/rename/rename.go +++ b/handler/file/rename/rename.go @@ -36,7 +36,14 @@ func PATCH(w http.ResponseWriter, r *http.Request) { return } - userFile, err := app.Server.Service.GetUserFile(r.Context(), newFile.Name, newFile.OwnerID.String()) + err = app.Server.Service.DeleteFileCache(r.Context(), fileID) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + app.Server.Logger.Error(err.Error()) + return + } + + userFile, err := app.Server.Service.GetUserFile(r.Context(), newFile.ID) if err != nil { w.WriteHeader(http.StatusInternalServerError) app.Server.Logger.Error(err.Error()) diff --git a/handler/file/visibility/visibility.go b/handler/file/visibility/visibility.go index 9540e7c..1190ce4 100644 --- a/handler/file/visibility/visibility.go +++ b/handler/file/visibility/visibility.go @@ -29,7 +29,14 @@ func PUT(w http.ResponseWriter, r *http.Request) { return } - userFile, err := app.Server.Service.GetUserFile(r.Context(), file.Name, file.OwnerID.String()) + err = app.Server.Service.DeleteFileCache(r.Context(), fileID) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + app.Server.Logger.Error(err.Error()) + return + } + + userFile, err := app.Server.Service.GetUserFile(r.Context(), file.ID) if err != nil { w.WriteHeader(http.StatusInternalServerError) app.Server.Logger.Error(err.Error()) diff --git a/handler/user/user.go b/handler/user/user.go index 4b7abf6..e899514 100644 --- a/handler/user/user.go +++ b/handler/user/user.go @@ -197,7 +197,7 @@ func handlerWS(conn *websocket.Conn, userSession types.User) { continue } - userFile, err := app.Server.Service.GetUserFile(context.Background(), uploadNewFile.Name, userSession.UserID.String()) + userFile, err := app.Server.Service.GetUserFile(context.Background(), fileID) if err != nil { app.Server.Logger.Error(err.Error()) sendErrorResponse(conn, action.Action, "Unknown error") @@ -216,7 +216,7 @@ func handlerWS(conn *websocket.Conn, userSession types.User) { sendErrorResponse(conn, action.Action, "File Is Different") continue } - userFile, err := app.Server.Service.GetUserFile(context.Background(), file.Name, userSession.UserID.String()) + userFile, err := app.Server.Service.GetUserFile(context.Background(), file.ID) if err != nil { app.Server.Logger.Error(err.Error()) sendErrorResponse(conn, action.Action, "Unknown error") diff --git a/service/service.go b/service/service.go index 059c29e..3223e3a 100644 --- a/service/service.go +++ b/service/service.go @@ -110,6 +110,14 @@ func (r *Service) GetFile(ctx context.Context, id string) (*models.File, error) return &fileCache, nil } +func (r *Service) DeleteFileCache(ctx context.Context, id string) error { + err := r.cache.DeleteCache(ctx, "FileCache:"+id) + if err != nil { + return err + } + return nil +} + func (r *Service) GetFileChunks(ctx context.Context, fileID uuid.UUID, ownerID uuid.UUID, totalChunk uint64) (*types.FileState, error) { fileJSON, err := r.cache.GetCache(ctx, "FileChunkCache:"+fileID.String()) if err != nil { @@ -185,66 +193,31 @@ func (r *Service) UpdateFileChunk(ctx context.Context, fileID uuid.UUID, ownerID return nil } -func (r *Service) GetUserFile(ctx context.Context, name, ownerID string) (*types.FileData, error) { - cacheKey := "UserFileCache:" + ownerID + ":" + name - cachedFileData, err := r.cache.GetCache(ctx, cacheKey) - if err != nil { - if errors.Is(err, redis.Nil) { - fileData, err := r.db.GetUserFile(name, ownerID) - if err != nil { - return nil, err - } - - chunks, err := r.GetFileChunks(ctx, fileData.ID, fileData.OwnerID, fileData.TotalChunk) - if err != nil { - return nil, err - } - - data := &types.FileData{ - ID: fileData.ID, - OwnerID: fileData.OwnerID, - Name: fileData.Name, - Size: fileData.Size, - TotalChunk: fileData.TotalChunk, - StartHash: fileData.StartHash, - EndHash: fileData.EndHash, - Downloaded: fileData.Downloaded, - IsPrivate: fileData.IsPrivate, - Type: fileData.Type, - Done: chunks.Done, - Chunk: chunks.Chunk, - } - - fileDataToCache := &types.FileData{ - ID: fileData.ID, - OwnerID: fileData.OwnerID, - Name: fileData.Name, - Size: fileData.Size, - TotalChunk: fileData.TotalChunk, - StartHash: fileData.StartHash, - EndHash: fileData.EndHash, - Downloaded: fileData.Downloaded, - IsPrivate: fileData.IsPrivate, - Type: fileData.Type, - } - cachedFileDataJSON, err := json.Marshal(fileDataToCache) - if err == nil { - _ = r.cache.SetCache(ctx, cacheKey, cachedFileDataJSON, time.Minute*30) - } - return data, nil - } - return nil, err - } - var fileData types.FileData - err = json.Unmarshal([]byte(cachedFileData), &fileData) +func (r *Service) GetUserFile(ctx context.Context, fileID uuid.UUID) (*types.FileData, error) { + fileData, err := r.GetFile(ctx, fileID.String()) if err != nil { return nil, err } + chunks, err := r.GetFileChunks(ctx, fileData.ID, fileData.OwnerID, fileData.TotalChunk) if err != nil { return nil, err } - fileData.Done = chunks.Done - fileData.Chunk = chunks.Chunk - return &fileData, nil + + data := &types.FileData{ + ID: fileData.ID, + OwnerID: fileData.OwnerID, + Name: fileData.Name, + Size: fileData.Size, + TotalChunk: fileData.TotalChunk, + StartHash: fileData.StartHash, + EndHash: fileData.EndHash, + Downloaded: fileData.Downloaded, + IsPrivate: fileData.IsPrivate, + Type: fileData.Type, + Done: chunks.Done, + Chunk: chunks.Chunk, + } + + return data, nil } diff --git a/types/types.go b/types/types.go index 40bd7c9..3387497 100644 --- a/types/types.go +++ b/types/types.go @@ -94,7 +94,8 @@ type Services interface { GetUser(ctx context.Context, email string) (*models.User, error) DeleteUser(ctx context.Context, email string) error GetFile(ctx context.Context, id string) (*models.File, error) - GetUserFile(ctx context.Context, name, ownerID string) (*FileData, error) + DeleteFileCache(ctx context.Context, id string) error + GetUserFile(ctx context.Context, fileID uuid.UUID) (*FileData, error) GetUserStorageUsage(ctx context.Context, ownerID string) (uint64, error) GetFileChunks(ctx context.Context, fileID uuid.UUID, ownerID uuid.UUID, totalChunk uint64) (*FileState, error) UpdateFileChunk(ctx context.Context, fileID uuid.UUID, ownerID uuid.UUID, chunk string, totalChunk uint64) error