add attachments
This commit is contained in:
parent
c523d40342
commit
91f8dd0db5
4 changed files with 149 additions and 3 deletions
|
|
@ -33,7 +33,7 @@ public class Requests
|
|||
return true;
|
||||
}
|
||||
|
||||
private static async Task<bool> IsMemberOfDm(string id, string dmId)
|
||||
public static async Task<bool> IsMemberOfDm(string id, string dmId)
|
||||
{
|
||||
if (!IsSafeDmId(dmId)) return false;
|
||||
string memberPath = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members/{id};{DOMAIN}";
|
||||
|
|
@ -401,6 +401,41 @@ public class Requests
|
|||
msg.responded = responded ?? "";
|
||||
msg.reactions = "";
|
||||
|
||||
if (!string.IsNullOrEmpty(msg.attachment) && msg.attachment != "[]")
|
||||
{
|
||||
try
|
||||
{
|
||||
var attachmentsNode = System.Text.Json.Nodes.JsonNode.Parse(msg.attachment);
|
||||
if (attachmentsNode is System.Text.Json.Nodes.JsonArray arr)
|
||||
{
|
||||
string attDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/attachments/{nextId}";
|
||||
if (!Directory.Exists(attDir)) Directory.CreateDirectory(attDir);
|
||||
|
||||
for (int i = 0; i < arr.Count; i++)
|
||||
{
|
||||
var attObj = arr[i] as System.Text.Json.Nodes.JsonObject;
|
||||
if (attObj != null && attObj.ContainsKey("staging_id"))
|
||||
{
|
||||
string stagingId = attObj["staging_id"].ToString();
|
||||
string stagingPath = $"{ROOMS_DIR}/dms_staging/{stagingId}";
|
||||
if (Fs.Exists(stagingPath))
|
||||
{
|
||||
string attId = attObj["id"].ToString();
|
||||
string destPath = $"{attDir}/{attId}";
|
||||
System.IO.File.Move(stagingPath, destPath);
|
||||
}
|
||||
attObj.Remove("staging_id"); // Clean it up before saving
|
||||
}
|
||||
}
|
||||
msg.attachment = attachmentsNode.ToJsonString();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error processing attachments: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
await Fs.WriteFile($"{msgDir}/{nextId}", Encoding.UTF8.GetBytes(JsonSerializer.Serialize(msg, AppJsonSerializerContext.Default.Message)));
|
||||
await Fs.WriteFile($"{msgDir}/last", Encoding.UTF8.GetBytes(nextId.ToString()));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue