Skip to content

Commit 637943d

Browse files
committed
fix: middleware crash 302
1 parent 9e755dc commit 637943d

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

Jellyfin.Plugin.GetAvatar/Jellyfin.Plugin.GetAvatar.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<TargetFramework>net9.0</TargetFramework>
55
<RootNamespace>Jellyfin.Plugin.GetAvatar</RootNamespace>
66
<ImplicitUsings>enable</ImplicitUsings>
7-
<AssemblyVersion>1.4.0</AssemblyVersion>
8-
<FileVersion>1.4.0</FileVersion>
7+
<AssemblyVersion>1.4.1</AssemblyVersion>
8+
<FileVersion>1.4.1</FileVersion>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1010
<Nullable>enable</Nullable>
1111
<CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>

Jellyfin.Plugin.GetAvatar/ScriptInjectorMiddleware.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ public async Task InvokeAsync(HttpContext context)
5151

5252
await _next(context).ConfigureAwait(false);
5353

54+
if (context.Response.StatusCode is StatusCodes.Status304NotModified or StatusCodes.Status204NoContent)
55+
{
56+
context.Response.Body = originalBodyStream;
57+
return;
58+
}
59+
60+
if (context.Response.StatusCode < 200 || context.Response.StatusCode >= 300)
61+
{
62+
memoryStream.Seek(0, SeekOrigin.Begin);
63+
context.Response.Body = originalBodyStream;
64+
await memoryStream.CopyToAsync(originalBodyStream).ConfigureAwait(false);
65+
return;
66+
}
67+
5468
memoryStream.Seek(0, SeekOrigin.Begin);
5569

5670
var contentType = context.Response.ContentType ?? string.Empty;

Jellyfin.Plugin.GetAvatar/Services/AvatarService.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,25 +278,14 @@ public async Task SetUserAvatarAsync(Guid userId, string avatarId, string? authT
278278

279279
try
280280
{
281-
// Delete old profile image file if exists
281+
// Remember the old path before making changes
282+
string? oldProfileImagePath = null;
282283
if (user.ProfileImage != null && !string.IsNullOrEmpty(user.ProfileImage.Path))
283284
{
284-
var oldPath = user.ProfileImage.Path;
285-
if (File.Exists(oldPath))
286-
{
287-
try
288-
{
289-
File.Delete(oldPath);
290-
_logger.LogInformation("Deleted old profile image: {Path}", oldPath);
291-
}
292-
catch (Exception ex)
293-
{
294-
_logger.LogWarning(ex, "Could not delete old profile image");
295-
}
296-
}
285+
oldProfileImagePath = user.ProfileImage.Path;
297286
}
298287

299-
// Copy the new avatar file
288+
// Copy the new avatar file first (before deleting anything)
300289
File.Copy(avatarPath, profileImagePath, overwrite: true);
301290
_logger.LogInformation("Copied avatar to profile path");
302291

@@ -314,6 +303,22 @@ public async Task SetUserAvatarAsync(Guid userId, string avatarId, string? authT
314303

315304
await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
316305
_logger.LogInformation("Saved user changes to database");
306+
307+
// Delete old profile image file after the database update succeeded
308+
if (oldProfileImagePath != null
309+
&& oldProfileImagePath != profileImagePath
310+
&& File.Exists(oldProfileImagePath))
311+
{
312+
try
313+
{
314+
File.Delete(oldProfileImagePath);
315+
_logger.LogInformation("Deleted old profile image: {Path}", oldProfileImagePath);
316+
}
317+
catch (Exception ex)
318+
{
319+
_logger.LogWarning(ex, "Could not delete old profile image (orphan file left)");
320+
}
321+
}
317322
}
318323
catch (Exception ex)
319324
{

0 commit comments

Comments
 (0)