Compare commits
2 Commits
92b28d830d
...
fa1a40c637
| Author | SHA1 | Date | |
|---|---|---|---|
|
fa1a40c637
|
|||
|
d43ce7cb11
|
@@ -16,7 +16,8 @@ public class FileController(
|
|||||||
FileService fs,
|
FileService fs,
|
||||||
QuotaService qs,
|
QuotaService qs,
|
||||||
IConfiguration configuration,
|
IConfiguration configuration,
|
||||||
IWebHostEnvironment env
|
IWebHostEnvironment env,
|
||||||
|
FileReferenceService fileReferenceService
|
||||||
) : ControllerBase
|
) : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
@@ -231,6 +232,21 @@ public class FileController(
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}/references")]
|
||||||
|
public async Task<ActionResult<List<Shared.Models.CloudFileReference>>> GetFileReferences(string id)
|
||||||
|
{
|
||||||
|
var file = await fs.GetFileAsync(id);
|
||||||
|
if (file is null) return NotFound("File not found.");
|
||||||
|
|
||||||
|
// Check if user has access to the file
|
||||||
|
var accessResult = await ValidateFileAccess(file, null);
|
||||||
|
if (accessResult is not null) return accessResult;
|
||||||
|
|
||||||
|
// Get references using the injected FileReferenceService
|
||||||
|
var references = await fileReferenceService.GetReferencesAsync(id);
|
||||||
|
return Ok(references);
|
||||||
|
}
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpPatch("{id}/name")]
|
[HttpPatch("{id}/name")]
|
||||||
public async Task<ActionResult<SnCloudFile>> UpdateFileName(string id, [FromBody] string name)
|
public async Task<ActionResult<SnCloudFile>> UpdateFileName(string id, [FromBody] string name)
|
||||||
@@ -348,110 +364,4 @@ public class FileController(
|
|||||||
var count = await fs.DeleteAllRecycledFilesAsync();
|
var count = await fs.DeleteAllRecycledFilesAsync();
|
||||||
return Ok(new { Count = count });
|
return Ok(new { Count = count });
|
||||||
}
|
}
|
||||||
|
|
||||||
public class CreateFastFileRequest
|
|
||||||
{
|
|
||||||
public string Name { get; set; } = null!;
|
|
||||||
public long Size { get; set; }
|
|
||||||
public string Hash { get; set; } = null!;
|
|
||||||
public string? MimeType { get; set; }
|
|
||||||
public string? Description { get; set; }
|
|
||||||
public Dictionary<string, object?>? UserMeta { get; set; }
|
|
||||||
public Dictionary<string, object?>? FileMeta { get; set; }
|
|
||||||
public List<Shared.Models.ContentSensitiveMark>? SensitiveMarks { get; set; }
|
|
||||||
public Guid PoolId { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
[Authorize]
|
|
||||||
[HttpPost("fast")]
|
|
||||||
[RequiredPermission("global", "files.create")]
|
|
||||||
public async Task<ActionResult<SnCloudFile>> CreateFastFile([FromBody] CreateFastFileRequest request)
|
|
||||||
{
|
|
||||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
|
||||||
var accountId = Guid.Parse(currentUser.Id);
|
|
||||||
|
|
||||||
var pool = await db.Pools.FirstOrDefaultAsync(p => p.Id == request.PoolId);
|
|
||||||
if (pool is null) return BadRequest();
|
|
||||||
if (!currentUser.IsSuperuser && pool.AccountId != accountId)
|
|
||||||
return StatusCode(403, "You don't have permission to create files in this pool.");
|
|
||||||
|
|
||||||
if (!pool.PolicyConfig.EnableFastUpload)
|
|
||||||
return StatusCode(
|
|
||||||
403,
|
|
||||||
"This pool does not allow fast upload"
|
|
||||||
);
|
|
||||||
|
|
||||||
if (pool.PolicyConfig.RequirePrivilege > 0)
|
|
||||||
{
|
|
||||||
if (currentUser.PerkSubscription is null)
|
|
||||||
{
|
|
||||||
return StatusCode(
|
|
||||||
403,
|
|
||||||
$"You need to have join the Stellar Program to use this pool"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
var privilege =
|
|
||||||
PerkSubscriptionPrivilege.GetPrivilegeFromIdentifier(currentUser.PerkSubscription.Identifier);
|
|
||||||
if (privilege < pool.PolicyConfig.RequirePrivilege)
|
|
||||||
{
|
|
||||||
return StatusCode(
|
|
||||||
403,
|
|
||||||
$"You need Stellar Program tier {pool.PolicyConfig.RequirePrivilege} to use this pool, you are tier {privilege}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.Size > pool.PolicyConfig.MaxFileSize)
|
|
||||||
{
|
|
||||||
return StatusCode(
|
|
||||||
403,
|
|
||||||
$"File size {request.Size} is larger than the pool's maximum file size {pool.PolicyConfig.MaxFileSize}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
var (ok, billableUnit, quota) = await qs.IsFileAcceptable(
|
|
||||||
accountId,
|
|
||||||
pool.BillingConfig.CostMultiplier ?? 1.0,
|
|
||||||
request.Size
|
|
||||||
);
|
|
||||||
if (!ok)
|
|
||||||
{
|
|
||||||
return StatusCode(
|
|
||||||
403,
|
|
||||||
$"File size {billableUnit} is larger than the user's quota {quota}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await using var transaction = await db.Database.BeginTransactionAsync();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var file = new SnCloudFile
|
|
||||||
{
|
|
||||||
Name = request.Name,
|
|
||||||
Size = request.Size,
|
|
||||||
Hash = request.Hash,
|
|
||||||
MimeType = request.MimeType,
|
|
||||||
Description = request.Description,
|
|
||||||
AccountId = accountId,
|
|
||||||
UserMeta = request.UserMeta,
|
|
||||||
FileMeta = request.FileMeta,
|
|
||||||
SensitiveMarks = request.SensitiveMarks,
|
|
||||||
PoolId = request.PoolId
|
|
||||||
};
|
|
||||||
db.Files.Add(file);
|
|
||||||
await db.SaveChangesAsync();
|
|
||||||
await fs._PurgeCacheAsync(file.Id);
|
|
||||||
await transaction.CommitAsync();
|
|
||||||
|
|
||||||
file.FastUploadLink = await fs.CreateFastUploadLinkAsync(file);
|
|
||||||
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
await transaction.RollbackAsync();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user