Resolve issue with file visibility not updating after caching user files
This commit is contained in:
@ -21,7 +21,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
|||||||
var filesData []types.FileData
|
var filesData []types.FileData
|
||||||
|
|
||||||
for _, file := range files {
|
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 {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
app.Server.Logger.Error(err.Error())
|
app.Server.Logger.Error(err.Error())
|
||||||
|
@ -31,7 +31,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
|||||||
var filesData []types.FileData
|
var filesData []types.FileData
|
||||||
|
|
||||||
for _, file := range files {
|
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 {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
app.Server.Logger.Error(err.Error())
|
app.Server.Logger.Error(err.Error())
|
||||||
|
@ -36,7 +36,14 @@ func PATCH(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
app.Server.Logger.Error(err.Error())
|
app.Server.Logger.Error(err.Error())
|
||||||
|
@ -29,7 +29,14 @@ func PUT(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
app.Server.Logger.Error(err.Error())
|
app.Server.Logger.Error(err.Error())
|
||||||
|
@ -197,7 +197,7 @@ func handlerWS(conn *websocket.Conn, userSession types.User) {
|
|||||||
continue
|
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 {
|
if err != nil {
|
||||||
app.Server.Logger.Error(err.Error())
|
app.Server.Logger.Error(err.Error())
|
||||||
sendErrorResponse(conn, action.Action, "Unknown 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")
|
sendErrorResponse(conn, action.Action, "File Is Different")
|
||||||
continue
|
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 {
|
if err != nil {
|
||||||
app.Server.Logger.Error(err.Error())
|
app.Server.Logger.Error(err.Error())
|
||||||
sendErrorResponse(conn, action.Action, "Unknown error")
|
sendErrorResponse(conn, action.Action, "Unknown error")
|
||||||
|
@ -110,6 +110,14 @@ func (r *Service) GetFile(ctx context.Context, id string) (*models.File, error)
|
|||||||
return &fileCache, nil
|
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) {
|
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())
|
fileJSON, err := r.cache.GetCache(ctx, "FileChunkCache:"+fileID.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -185,66 +193,31 @@ func (r *Service) UpdateFileChunk(ctx context.Context, fileID uuid.UUID, ownerID
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Service) GetUserFile(ctx context.Context, name, ownerID string) (*types.FileData, error) {
|
func (r *Service) GetUserFile(ctx context.Context, fileID uuid.UUID) (*types.FileData, error) {
|
||||||
cacheKey := "UserFileCache:" + ownerID + ":" + name
|
fileData, err := r.GetFile(ctx, fileID.String())
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
chunks, err := r.GetFileChunks(ctx, fileData.ID, fileData.OwnerID, fileData.TotalChunk)
|
chunks, err := r.GetFileChunks(ctx, fileData.ID, fileData.OwnerID, fileData.TotalChunk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
fileData.Done = chunks.Done
|
|
||||||
fileData.Chunk = chunks.Chunk
|
data := &types.FileData{
|
||||||
return &fileData, nil
|
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
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,8 @@ type Services interface {
|
|||||||
GetUser(ctx context.Context, email string) (*models.User, error)
|
GetUser(ctx context.Context, email string) (*models.User, error)
|
||||||
DeleteUser(ctx context.Context, email string) error
|
DeleteUser(ctx context.Context, email string) error
|
||||||
GetFile(ctx context.Context, id string) (*models.File, 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)
|
GetUserStorageUsage(ctx context.Context, ownerID string) (uint64, error)
|
||||||
GetFileChunks(ctx context.Context, fileID uuid.UUID, ownerID uuid.UUID, totalChunk uint64) (*FileState, 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
|
UpdateFileChunk(ctx context.Context, fileID uuid.UUID, ownerID uuid.UUID, chunk string, totalChunk uint64) error
|
||||||
|
Reference in New Issue
Block a user