Blogs: fix delete returning 40x despite the file being removed

DeleteUserDirOrFile (and DeleteUserFile) read FileInfo.Length AFTER
fi.Delete(), and FileInfo.Length throws FileNotFoundException once
the file is gone. The controller's catch turned that into a 400, so
the user saw "le fichier n'existe pas sur le serveur" while the delete
had in fact succeeded. Capture the length before deleting.

Adds FileSystemApiTests.Delete_fs_removes_the_file_and_returns_200
(asserts 200 + file gone from disk). 8/8 FileSystemApiTests pass.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
Paul Schneider 2026-09-25 21:33:13 +01:00
commit 7844449c99
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
3 changed files with 53 additions and 2 deletions

View file

@ -70,8 +70,12 @@ namespace Yavsc.Server.Helpers
var root = Path.Combine(siteSettings.Blog, user.UserName);
var fi = new FileInfo(Path.Combine(root, fileName));
if (!fi.Exists) return;
// Capture the length BEFORE deleting: FileInfo.Length throws
// FileNotFoundException once the file is gone, which would
// surface as a 400 even though the delete already succeeded.
var length = fi.Length;
fi.Delete();
user.DiskUsage -= fi.Length;
user.DiskUsage -= length;
}
public static FsOperationInfo DeleteUserDirOrFile(this ApplicationUser user, string dirName, SiteSettings siteSettings)
@ -85,8 +89,15 @@ namespace Yavsc.Server.Helpers
{
var fi = new FileInfo(Path.Combine(root, dirName));
if (!fi.Exists) return new FsOperationInfo { Done = false, ErrorCode = ErrorCode.NotFound, ErrorMessage = "non existent" };
// Capture the length BEFORE deleting: FileInfo.Length
// throws FileNotFoundException once the file is gone, so
// reading it after fi.Delete() would throw — the caller's
// catch turns that into a 400 even though the file was
// already removed (the user sees "file does not exist"
// while the delete in fact succeeded).
var length = fi.Length;
fi.Delete();
user.DiskUsage -= fi.Length;
user.DiskUsage -= length;
}
else
{