Skip to content

Commit b9ac475

Browse files
committed
Add MigrateOldArtifacts API
1 parent db1bcbc commit b9ac475

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using BlazorDiffusion.ServiceInterface.AiServer;
5+
using BlazorDiffusion.ServiceModel;
6+
using Microsoft.Extensions.Logging;
7+
using ServiceStack;
8+
using ServiceStack.OrmLite;
9+
10+
namespace BlazorDiffusion.ServiceInterface;
11+
12+
[ValidateApiKey]
13+
public class MigrateArtifact : IPost, IReturn<MigrateArtifactResponse>
14+
{
15+
public string Path { get; set; }
16+
public DateTime? Date { get; set; }
17+
}
18+
public class MigrateArtifactResponse
19+
{
20+
public string FilePath { get; set; }
21+
public ResponseStatus? ResponseStatus { get; set; }
22+
}
23+
24+
public class MigrationServices(ILogger<MigrationServices> log, AiServerClient aiClient) : Service
25+
{
26+
public async Task<object> Any(MigrateOldArtifacts request)
27+
{
28+
var limit = request.Limit ?? 10;
29+
var artifacts = Db.Select(Db.From<Artifact>()
30+
.Where(x => x.FilePath.EndsWith(".png") || !x.FilePathSmall!.StartsWith("/variant"))
31+
.OrderBy(x => x.Id)
32+
.Take(limit));
33+
34+
var to = new MigrateOldArtifactsResponse();
35+
foreach (var artifact in artifacts)
36+
{
37+
try
38+
{
39+
var filePath = artifact.FilePath;
40+
if (filePath.EndsWith(".png"))
41+
filePath = filePath.LastLeftPart('.') + ".webp";
42+
var api = await aiClient.Client.ApiAsync(new MigrateArtifact
43+
{
44+
Path = filePath,
45+
Date = artifact.CreatedDate,
46+
});
47+
if (api.Succeeded)
48+
{
49+
lock (Locks.AppDb)
50+
{
51+
var newFilePath = api.Response?.FilePath;
52+
if (string.IsNullOrEmpty(newFilePath))
53+
{
54+
to.Failed.Add(filePath);
55+
}
56+
else
57+
{
58+
var variant = artifact.Height > artifact.Width ? "height" : "width";
59+
var fileName = newFilePath.LastRightPart('/');
60+
var filePathSmall = $"/variants/{variant}=118".CombineWith(newFilePath.RightPart("/artifacts"));
61+
var filePathMedium = $"/variants/{variant}=288".CombineWith(newFilePath.RightPart("/artifacts"));
62+
Db.UpdateOnly(() => new Artifact
63+
{
64+
FileName = fileName,
65+
FilePath = newFilePath,
66+
FilePathSmall = filePathSmall,
67+
FilePathMedium = filePathMedium,
68+
FilePathLarge = newFilePath,
69+
}, where: a => a.Id == artifact.Id);
70+
}
71+
}
72+
}
73+
else
74+
{
75+
to.Failed.Add(artifact.FilePath);
76+
}
77+
}
78+
catch (Exception e)
79+
{
80+
log.LogError(e, "Failed to migrate Artifact {Id} at {Path}: {Message}", artifact.Id, artifact.FilePath, e.Message);
81+
to.Failed.Add(artifact.FilePath);
82+
}
83+
}
84+
85+
return to;
86+
}
87+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using ServiceStack;
3+
4+
namespace BlazorDiffusion.ServiceModel;
5+
6+
public class MigrateOldArtifacts : IGet, IReturn<MigrateOldArtifactsResponse>
7+
{
8+
public int? Limit { get; set; }
9+
}
10+
public class MigrateOldArtifactsResponse
11+
{
12+
public List<string> Results { get; set; } = [];
13+
public List<string> Failed { get; set; } = [];
14+
public ResponseStatus? ResponseStatus { get; set; }
15+
}

0 commit comments

Comments
 (0)