Overwrite existing files if `AddFile` is called with an existing filename

This commit is contained in:
Dean Herbert 2021-12-02 17:17:12 +09:00
parent ec700e9142
commit ae3038ead4
2 changed files with 10 additions and 2 deletions

View File

@ -25,7 +25,7 @@ public interface IModelFileManager<in TModel, in TFileModel>
void DeleteFile(TModel model, TFileModel file);
/// <summary>
/// Add a new file.
/// Add a new file. If the file already exists, it is overwritten.
/// </summary>
/// <param name="model">The item to operate on.</param>
/// <param name="contents">The new file contents.</param>

View File

@ -74,10 +74,18 @@ protected void ReplaceFile(TModel model, RealmNamedFileUsage file, Stream conten
}
/// <summary>
/// Add a file from within an ongoing realm transaction.
/// Add a file from within an ongoing realm transaction. If the file already exists, it is overwritten.
/// </summary>
protected void AddFile(TModel item, Stream stream, string filename, Realm realm)
{
var existing = item.Files.FirstOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase));
if (existing != null)
{
ReplaceFile(item, existing, stream, realm);
return;
}
var file = realmFileStore.Add(stream, realm);
var namedUsage = new RealmNamedFileUsage(file, filename);