add attachments
This commit is contained in:
parent
c523d40342
commit
91f8dd0db5
4 changed files with 149 additions and 3 deletions
|
|
@ -863,6 +863,87 @@ public static async Task Auth(HttpContext context, Func<Task> next, IQueryCollec
|
|||
userLock.Release();
|
||||
}
|
||||
}
|
||||
public static async Task UploadDmAttachmentStaging(HttpContext context, Func<Task> next, IQueryCollection query)
|
||||
{
|
||||
if (!query.TryGetValue("id", out var idQuery)) return;
|
||||
if (!context.Request.Headers.TryGetValue("secret", out var secret)) return;
|
||||
|
||||
string id = Utils.GetValidIdOrZero(idQuery.ToString());
|
||||
string password = await Utils.GetPassword(id);
|
||||
string secretStr = await Utils.NonceDecryptBody(id, password, secret, false);
|
||||
string auth = await Utils.Auth(id, secretStr, password);
|
||||
|
||||
if (auth != Utils.LOGIN_SUCCESS)
|
||||
{
|
||||
await context.Response.WriteAsync(Encryption.Encryption.EncryptString(auth, secretStr));
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsActionRateLimited(id))
|
||||
{
|
||||
await context.Response.WriteAsync(Encryption.Encryption.EncryptString("error:rate.limit", password));
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] fileBytes = await LarpixServer.Utils.Utils.LoadBodyBytes(context.Request.Body, 50 * 1024 * 1024); // 50MB limit for attachments
|
||||
if (fileBytes == null || fileBytes.Length == 0)
|
||||
{
|
||||
await context.Response.WriteAsync(Encryption.Encryption.EncryptString("error:empty.body", password));
|
||||
return;
|
||||
}
|
||||
|
||||
string stagingId = Guid.NewGuid().ToString();
|
||||
string stagingDir = $"{LarpixServer.Utils.Utils.ROOMS_DIR}/dms_staging";
|
||||
string filePath = $"{stagingDir}/{stagingId}";
|
||||
|
||||
await Fs.WriteFile(filePath, fileBytes);
|
||||
|
||||
// Fire and forget 10 min timer to delete
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMinutes(10));
|
||||
if (Fs.Exists(filePath))
|
||||
{
|
||||
Fs.DeleteFile(filePath);
|
||||
}
|
||||
});
|
||||
|
||||
await context.Response.WriteAsync(Encryption.Encryption.EncryptString($"success:{stagingId}", password));
|
||||
}
|
||||
|
||||
public static async Task GetDmAttachment(HttpContext context, Func<Task> next, IQueryCollection query)
|
||||
{
|
||||
if (!query.TryGetValue("id", out var idQuery)) return;
|
||||
if (!query.TryGetValue("dm", out var dmQuery)) return;
|
||||
if (!query.TryGetValue("msg", out var msgQuery)) return;
|
||||
if (!query.TryGetValue("att", out var attQuery)) return;
|
||||
if (!context.Request.Headers.TryGetValue("secret", out var secret)) return;
|
||||
|
||||
string id = Utils.GetValidIdOrZero(idQuery.ToString());
|
||||
string password = await Utils.GetPassword(id);
|
||||
string secretStr = await Utils.NonceDecryptBody(id, password, secret, false);
|
||||
string auth = await Utils.Auth(id, secretStr, password);
|
||||
|
||||
if (auth != Utils.LOGIN_SUCCESS) return; // Don't leak info
|
||||
|
||||
string dmId = dmQuery.ToString();
|
||||
string msgId = msgQuery.ToString();
|
||||
string attId = attQuery.ToString();
|
||||
|
||||
// Check if user is in DM
|
||||
if (!await Room.Requests.IsMemberOfDm(id, dmId)) return;
|
||||
|
||||
string filePath = $"{LarpixServer.Utils.Utils.ROOMS_DIR}/dms/{LarpixServer.Utils.Utils.DOMAIN}/{dmId}/attachments/{msgId}/{attId}";
|
||||
if (!Fs.Exists(filePath))
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] fileBytes = await Fs.ReadFile(filePath);
|
||||
context.Response.ContentType = "application/octet-stream";
|
||||
await context.Response.Body.WriteAsync(fileBytes, 0, fileBytes.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateHolder
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue