V2-4 : placements multi-device (report_bytes, tables 000015, copies UI)

This commit is contained in:
venture
2026-09-25 19:45:33 +02:00
parent 0f90aad938
commit a91ddf7aa8
42 changed files with 714 additions and 25 deletions
+65
View File
@@ -900,3 +900,68 @@ func TestFilesSharesLinksListing(t *testing.T) {
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/shares", ownerToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-invalid-id")
}
func TestFilesPlacementsListing(t *testing.T) {
r, _, repo := setup(t)
ownerDevice := repository.NewID()
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "pl"), "placements-password", ownerDevice)
granteeDevice := repository.NewID()
granteeToken, grantee := registerAndLogin(t, r, repo, testUserUsername(granteeDevice, "pg"), "placements-password-b", granteeDevice)
outsiderDevice := repository.NewID()
outsiderToken, _ := registerAndLogin(t, r, repo, testUserUsername(outsiderDevice, "ph"), "placements-password-c", outsiderDevice)
fileID := repository.NewID()
if err := repo.Resources.InsertFile(owner, fileID, "placed.txt", "", 4, nil, nil); err != nil {
t.Fatalf("insert file: %v", err)
}
// Sans report : seule l'entrée serveur (hasContent false, metadata-only).
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", ownerToken, nil, "")
env := expectOK(t, rec, "placements-server-only")
var only []map[string]any
if err := json.Unmarshal(env.Data, &only); err != nil {
t.Fatalf("placements-server-only: unmarshal: %v", err)
}
if len(only) != 1 || only[0]["locationType"] != "server" || only[0]["hasBytes"] != false {
t.Errorf("placements-server-only = %v, attendu 1 entrée serveur false", only)
}
// Report via l'outbox (owner) : l'entrée device apparaît.
reportBody := fmt.Sprintf(
`{"operations":[{"operation_id":%q,"resource_id":%q,"resource_type":"file","operation":"report_bytes","payload":{"hasBytes":true}}]}`,
repository.NewID(), fileID,
)
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/sync/ops", ownerToken, []byte(reportBody), "application/json")
if rec.Code != http.StatusOK {
t.Fatalf("sync/ops report: attendu 200, got %d body=%s", rec.Code, rec.Body.String())
}
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", ownerToken, nil, "")
env = expectOK(t, rec, "placements-owner")
var entries []struct {
LocationType string `json:"locationType"`
DeviceID string `json:"deviceId"`
HasBytes bool `json:"hasBytes"`
}
if err := json.Unmarshal(env.Data, &entries); err != nil {
t.Fatalf("placements-owner: unmarshal: %v", err)
}
if len(entries) != 2 || entries[0].LocationType != "server" {
t.Fatalf("placements-owner = %+v, attendu serveur + 1 device", entries)
}
if entries[1].LocationType != "mobile" || !entries[1].HasBytes || entries[1].DeviceID != ownerDevice {
t.Errorf("placements-owner[1] = %+v, attendu device owner true", entries[1])
}
// Granté et outsider : 404 (owner-only, pas d'énumération).
if err := repo.Shares.Upsert(fileID, grantee, "viewer", false, nil, owner); err != nil {
t.Fatalf("share: %v", err)
}
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", granteeToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "placements-grantee")
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", outsiderToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "placements-outsider")
// ID invalide → 404.
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/placements", ownerToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "placements-invalid-id")
}
+1
View File
@@ -36,6 +36,7 @@ func RegisterRoutes(r *gin.Engine) {
protected.GET("/files/:id/content", FilesContent)
protected.GET("/files/:id/shares", SharesList)
protected.GET("/files/:id/links", LinksList)
protected.GET("/files/:id/placements", PlacementsList)
protected.DELETE("/files/:id", FilesDelete)
protected.POST("/files/upload", FilesUpload)
+22
View File
@@ -50,3 +50,25 @@ func LinksList(c *gin.Context) {
}
api.OK(c, links)
}
// PlacementsList exposes byte presence of a resource the caller OWNS
// (server entry + device reports, newest first) : GET /files/:id/placements.
// Owner-only (404 otherwise, no enumeration).
func PlacementsList(c *gin.Context) {
if Store == nil {
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
return
}
userID := c.GetString(UserIDKey)
id := c.Param("id")
if !deviceIDPattern.MatchString(id) {
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
return
}
placements, err := Store.ListPlacements(userID, id)
if err != nil {
writeError(c, err)
return
}
api.OK(c, placements)
}