[backend] Download exported follow list directly instead of storing it as a file

This commit is contained in:
pancakes 2024-10-28 12:48:35 +10:00 committed by Laura Hausmann
parent 137dc0d0e6
commit 95ae04e4af
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 8 additions and 17 deletions

View file

@ -1,5 +1,6 @@
using System.Net; using System.Net;
using System.Net.Mime; using System.Net.Mime;
using System.Text;
using AngleSharp.Text; using AngleSharp.Text;
using Iceshrimp.Backend.Controllers.Shared.Attributes; using Iceshrimp.Backend.Controllers.Shared.Attributes;
using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database;
@ -68,9 +69,9 @@ public class SettingsController(DatabaseContext db, ImportExportService importEx
} }
[HttpPost("export/following")] [HttpPost("export/following")]
[ProducesResults(HttpStatusCode.Accepted)] [ProducesResults(HttpStatusCode.OK)]
[ProducesErrors(HttpStatusCode.BadRequest)] [ProducesErrors(HttpStatusCode.BadRequest)]
public async Task<AcceptedResult> ExportFollowing() public async Task<FileContentResult> ExportFollowing()
{ {
var user = HttpContext.GetUserOrFail(); var user = HttpContext.GetUserOrFail();
@ -79,9 +80,9 @@ public class SettingsController(DatabaseContext db, ImportExportService importEx
if (followCount < 1) if (followCount < 1)
throw GracefulException.BadRequest("You do not follow any users"); throw GracefulException.BadRequest("You do not follow any users");
await importExportSvc.ExportFollowingAsync(user); var following = await importExportSvc.ExportFollowingAsync(user);
return Accepted(); return File(Encoding.UTF8.GetBytes(following), "text/csv", $"following-{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.csv");
} }
[HttpPost("import/following")] [HttpPost("import/following")]

View file

@ -1,4 +1,3 @@
using System.Text;
using Iceshrimp.Backend.Core.Configuration; using Iceshrimp.Backend.Core.Configuration;
using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
@ -14,12 +13,11 @@ public class ImportExportService(
ILogger<UserService> logger, ILogger<UserService> logger,
IOptions<Config.InstanceSection> instance, IOptions<Config.InstanceSection> instance,
CacheService cacheSvc, CacheService cacheSvc,
DriveService driveSvc,
UserService userSvc, UserService userSvc,
ActivityPub.UserResolver userResolver ActivityPub.UserResolver userResolver
) )
{ {
public async Task ExportFollowingAsync(User user) public async Task<string> ExportFollowingAsync(User user)
{ {
var followees = await db.Followings var followees = await db.Followings
.Include(p => p.Followee) .Include(p => p.Followee)
@ -31,15 +29,7 @@ public class ImportExportService(
.Select(p => p.GetFqn(instance.Value.AccountDomain)) .Select(p => p.GetFqn(instance.Value.AccountDomain))
.ToListAsync(); .ToListAsync();
var stream = new MemoryStream(Encoding.UTF8.GetBytes(string.Join("\n", followees))); return string.Join("\n", followees);
await driveSvc.StoreFile(stream, user,
new DriveFileCreationRequest
{
Filename = $"following-{DateTime.UtcNow:yyyy-MM-dd-HH-mm-ss}.csv",
IsSensitive = false,
MimeType = "text/csv"
}, true);
} }
public async Task ImportFollowingAsync(User user, List<string> fqns) public async Task ImportFollowingAsync(User user, List<string> fqns)