retention and replace
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
package content
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/document"
|
||||
"mediarr/ident"
|
||||
"mediarr/permission"
|
||||
"mediarr/storage"
|
||||
)
|
||||
|
||||
type GarbageCollectionResult struct {
|
||||
Blobs int
|
||||
Locations int
|
||||
Bytes int64
|
||||
}
|
||||
|
||||
func (s *Service) CollectGarbage(ctx context.Context) (GarbageCollectionResult, error) {
|
||||
if s.store == nil {
|
||||
return GarbageCollectionResult{}, ErrStorageNotConfigured
|
||||
}
|
||||
|
||||
var blobs []database.Blob
|
||||
var locations []database.BlobLocation
|
||||
var totalBytes int64
|
||||
|
||||
err := s.withTx(ctx, func(q *database.Queries) error {
|
||||
var err error
|
||||
blobs, err = q.ListUnreferencedBlobs(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, blob := range blobs {
|
||||
totalBytes += blob.Size
|
||||
|
||||
blobLocations, err := q.ListBlobLocations(ctx, blob.Root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
locations = append(locations, blobLocations...)
|
||||
|
||||
if err := q.DeleteBlob(ctx, blob.Root); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return GarbageCollectionResult{}, err
|
||||
}
|
||||
|
||||
result := GarbageCollectionResult{
|
||||
Blobs: len(blobs),
|
||||
Bytes: totalBytes,
|
||||
}
|
||||
|
||||
var removeErrors []error
|
||||
for _, location := range locations {
|
||||
if err := s.store.Remove(ctx, location.Locator); err != nil {
|
||||
removeErrors = append(removeErrors, err)
|
||||
continue
|
||||
}
|
||||
result.Locations++
|
||||
}
|
||||
|
||||
if len(removeErrors) > 0 {
|
||||
return result, errors.Join(removeErrors...)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Service) VerifyVersion(ctx context.Context, actorID, versionID ident.UUID) error {
|
||||
if s.store == nil {
|
||||
return ErrStorageNotConfigured
|
||||
}
|
||||
|
||||
version, err := s.getVersion(ctx, versionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
documentRow, err := s.getDocument(ctx, version.DocumentID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if documentRow.DeletedAt.Valid {
|
||||
return fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentRow.ID)
|
||||
}
|
||||
if err := s.can(ctx, actorID, version.DocumentID, permission.Read); err != nil {
|
||||
return err
|
||||
}
|
||||
if version.ContentPurgedAt.Valid {
|
||||
return fmt.Errorf("%w: %s", ErrContentPurged, versionID)
|
||||
}
|
||||
|
||||
if _, err := s.db.Queries.GetBlob(ctx, version.ContentRoot); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
location, err := s.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||
Root: version.ContentRoot,
|
||||
BackendID: s.backendID,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||
}
|
||||
return err
|
||||
}
|
||||
if location.State != BlobLocationPresent {
|
||||
return fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||
}
|
||||
|
||||
reader, err := s.store.Open(ctx, location.Locator)
|
||||
if err != nil {
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
unavailable := fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||
updateErr := s.db.WriteQueries.UpdateBlobLocationState(ctx, database.UpdateBlobLocationStateParams{
|
||||
State: BlobLocationMissing,
|
||||
Root: version.ContentRoot,
|
||||
BackendID: s.backendID,
|
||||
})
|
||||
if updateErr != nil {
|
||||
return errors.Join(unavailable, updateErr)
|
||||
}
|
||||
return unavailable
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
hasher := sha256.New()
|
||||
size, readErr := io.Copy(hasher, reader)
|
||||
closeErr := reader.Close()
|
||||
if readErr != nil {
|
||||
return readErr
|
||||
}
|
||||
if closeErr != nil {
|
||||
return closeErr
|
||||
}
|
||||
|
||||
now := nowMillis()
|
||||
if size != version.Size || !bytes.Equal(hasher.Sum(nil), version.ContentRoot) {
|
||||
corrupt := fmt.Errorf("%w: %s", ErrBlobCorrupt, versionID)
|
||||
updateErr := s.db.WriteQueries.UpdateBlobVerification(ctx, database.UpdateBlobVerificationParams{
|
||||
State: BlobStateCorrupt,
|
||||
LastVerifiedAt: sql.NullInt64{Int64: now, Valid: true},
|
||||
VerifyError: sql.NullString{String: "empreinte ou taille incohérente", Valid: true},
|
||||
Root: version.ContentRoot,
|
||||
})
|
||||
if updateErr != nil {
|
||||
return errors.Join(corrupt, updateErr)
|
||||
}
|
||||
return corrupt
|
||||
}
|
||||
|
||||
return s.db.WriteQueries.UpdateBlobVerification(ctx, database.UpdateBlobVerificationParams{
|
||||
State: BlobStatePresent,
|
||||
LastVerifiedAt: sql.NullInt64{Int64: now, Valid: true},
|
||||
VerifyError: sql.NullString{},
|
||||
Root: version.ContentRoot,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user