[backend/drive] Store attachments as links if they can't be fetched for caching / further processing

This commit is contained in:
Laura Hausmann 2024-05-16 18:43:24 +02:00
parent b76f154f50
commit 2108f91bcf
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -84,6 +84,8 @@ public class DriveService(
if (!logExisting) if (!logExisting)
logger.LogDebug("Storing file {uri} for user {userId}", uri, user.Id); logger.LogDebug("Storing file {uri} for user {userId}", uri, user.Id);
try
{
var res = await httpClient.GetAsync(uri); var res = await httpClient.GetAsync(uri);
var request = new DriveFileCreationRequest var request = new DriveFileCreationRequest
@ -98,6 +100,31 @@ public class DriveService(
return await StoreFile(await res.Content.ReadAsStreamAsync(), user, request); return await StoreFile(await res.Content.ReadAsStreamAsync(), user, request);
} }
catch (Exception e) catch (Exception e)
{
logger.LogDebug("Failed to download file from {uri}: {error}, storing as link", uri, e.Message);
file = new DriveFile
{
Id = IdHelpers.GenerateSlowflakeId(),
CreatedAt = DateTime.UtcNow,
User = user,
UserHost = user.Host,
Size = 0,
IsLink = true,
IsSensitive = sensitive,
StoredInternal = false,
Uri = uri,
Url = uri,
Name = new Uri(uri).AbsolutePath.Split('/').LastOrDefault() ?? "",
Comment = description,
Type = CleanMimeType(mimeType ?? "application/octet-stream")
};
db.Add(file);
await db.SaveChangesAsync();
return file;
}
}
catch (Exception e)
{ {
logger.LogError("Failed to insert file {uri}: {error}", uri, e.Message); logger.LogError("Failed to insert file {uri}: {error}", uri, e.Message);
return null; return null;