mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-03 07:06:36 +08:00
299ac30ee4
- Purify core micro-kernel by removing context hardcoded helpers and reverse dependencies - Eliminate init() side effects in infra plugins with reversible lifecycle disposal - Completely isolate plugins by removing cross-plugin imports and using core/contracts - Introduce TaskService and RiskControlService contracts for unified cross-plugin APIs - Regenerate Swagger documentation and update developer guide matrix - Achieve 0 violations in check_cordis_architecture.sh and 100% test pass
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package ingest
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"Wavelet/plugins/domain/upload/models"
|
|
"Wavelet/plugins/domain/upload/repository"
|
|
)
|
|
|
|
// Ingest stores or resolves an upload using the configured policy and side effects.
|
|
func Ingest(ctx context.Context, req Request) (Result, error) {
|
|
normalizeRequest(&req)
|
|
if req.Hash == "" {
|
|
return Result{}, errors.New("ingest hash is required")
|
|
}
|
|
if req.Reader == nil {
|
|
return Result{}, errors.New("ingest reader is required")
|
|
}
|
|
if req.Size < 0 {
|
|
return Result{}, errors.New("ingest size must be non-negative")
|
|
}
|
|
|
|
switch req.Policy {
|
|
case PolicyDedupNewRecord, PolicyResolveExisting:
|
|
return ingestWithHashPolicy(ctx, req)
|
|
case PolicyCreate:
|
|
return createNewUpload(ctx, req)
|
|
default:
|
|
return Result{}, errors.New("unsupported ingest policy")
|
|
}
|
|
}
|
|
|
|
// FindByHash returns a reusable active upload with the same hash and size.
|
|
func FindByHash(ctx context.Context, hash string, size int64) (models.Upload, error) {
|
|
return repository.FindReusableUploadByHash(ctx, hash, size)
|
|
}
|
|
|
|
func ingestWithHashPolicy(ctx context.Context, req Request) (Result, error) {
|
|
existing, err := repository.FindReusableUploadByHash(ctx, req.Hash, req.Size)
|
|
if err == nil {
|
|
switch req.Policy {
|
|
case PolicyResolveExisting:
|
|
return Result{
|
|
Upload: existing,
|
|
Resolved: true,
|
|
}, nil
|
|
case PolicyDedupNewRecord:
|
|
if uploadstorageReadOnly(ctx) {
|
|
return Result{}, ErrStorageReadOnly
|
|
}
|
|
return createDedupRecord(ctx, existing, req)
|
|
}
|
|
}
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return Result{}, err
|
|
}
|
|
|
|
return createNewUpload(ctx, req)
|
|
}
|