76 lines
2.1 KiB
C#
76 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FileService.Application.StorageContracts
|
|
{
|
|
public sealed class UploadRuntimeCache
|
|
{
|
|
public string TaskId { get; init; } = default!;
|
|
|
|
public string ProviderCode { get; init; } = default!;
|
|
|
|
public string UploadSessionId { get; init; } = default!;
|
|
|
|
public string Bucket { get; init; } = default!;
|
|
|
|
public string Region { get; init; } = default!;
|
|
|
|
public string ObjectKey { get; init; } = default!;
|
|
|
|
public long FileSize { get; init; }
|
|
|
|
public int TotalPartCount { get; init; }
|
|
|
|
public long UploadedBytes { get; set; }
|
|
|
|
public DateTimeOffset CreatedAt { get; init; }
|
|
|
|
public DateTimeOffset ExpireAt { get; init; }
|
|
|
|
public Dictionary<int, UploadPart> Parts { get; init; } = new();
|
|
public UploadRuntimeCache() { }
|
|
public UploadRuntimeCache(string taskId, string providerCode,
|
|
string uploadSessionId, string bucket, string region,
|
|
string objectKey, long fileSize, int totalPartCount,
|
|
DateTimeOffset? expireAt = null)
|
|
{
|
|
TaskId = taskId;
|
|
ProviderCode = providerCode;
|
|
UploadSessionId = uploadSessionId;
|
|
Bucket = bucket;
|
|
Region = region;
|
|
ObjectKey = objectKey;
|
|
FileSize = fileSize;
|
|
TotalPartCount = totalPartCount;
|
|
ExpireAt = expireAt ?? DateTimeOffset.UtcNow.AddHours(24);
|
|
CreatedAt = DateTime.Now;
|
|
}
|
|
|
|
public void AddOrUpdatePart(UploadPart part)
|
|
{
|
|
Parts[part.PartNumber] = part;
|
|
|
|
UploadedBytes = Parts.Sum(x => x.Value.Size ?? 0);
|
|
}
|
|
|
|
public bool IsCompleted()
|
|
{
|
|
return Parts.Count == TotalPartCount;
|
|
}
|
|
|
|
public string ToJson()
|
|
{
|
|
return JsonSerializer.Serialize(this);
|
|
}
|
|
|
|
public static UploadRuntimeCache? FromJson(string json)
|
|
{
|
|
return JsonSerializer.Deserialize<UploadRuntimeCache>(json);
|
|
}
|
|
}
|
|
}
|