Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a File.HasSubdirectories property #131

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class File : FullAuditedAggregateRoot<Guid>, IFile, IMultiTenant

public virtual int SubFilesQuantity { get; protected set; }

public virtual bool HasSubdirectories { get; protected set; }

public virtual long ByteSize { get; protected set; }

public virtual string Hash { get; protected set; }
Expand Down Expand Up @@ -50,7 +52,6 @@ internal File(
[NotNull] string fileName,
[CanBeNull] string mimeType,
FileType fileType,
int subFilesQuantity,
long byteSize,
[CanBeNull] string hash,
[CanBeNull] string blobName,
Expand All @@ -76,7 +77,8 @@ internal File(
FileName = fileName;
MimeType = mimeType;
FileType = fileType;
SubFilesQuantity = subFilesQuantity;
SubFilesQuantity = 0;
HasSubdirectories = false;
ByteSize = byteSize;
Hash = hash;
BlobName = blobName;
Expand All @@ -89,6 +91,7 @@ internal void UpdateInfo(
[NotNull] string fileName,
[CanBeNull] string mimeType,
int subFilesQuantity,
bool hasSubdirectories,
long byteSize,
[CanBeNull] string hash,
[CanBeNull] string blobName,
Expand All @@ -109,19 +112,23 @@ internal void UpdateInfo(
FileName = fileName;
MimeType = mimeType;
SubFilesQuantity = subFilesQuantity;
HasSubdirectories = hasSubdirectories;
ByteSize = byteSize;
Hash = hash;
BlobName = blobName;
}

public bool TryUpdateStatisticData(SubFilesStatisticDataModel statisticData)
{
if (statisticData.SubFilesQuantity == SubFilesQuantity && statisticData.ByteSize == ByteSize)
if (statisticData.SubFilesQuantity == SubFilesQuantity &&
statisticData.HasSubdirectories == HasSubdirectories &&
statisticData.ByteSize == ByteSize)
{
return false;
}

SubFilesQuantity = statisticData.SubFilesQuantity;
HasSubdirectories = statisticData.HasSubdirectories;
ByteSize = statisticData.ByteSize;

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ await CheckFileNotExistAsync(model.NewFileName, file.ParentId, file.FileContaine
file.OwnerUserId, true);
}

file.UpdateInfo(model.NewFileName, model.NewMimeType, file.SubFilesQuantity, file.ByteSize, file.Hash,
file.BlobName, parent);
file.UpdateInfo(model.NewFileName, model.NewMimeType, file.SubFilesQuantity, file.HasSubdirectories,
file.ByteSize, file.Hash, file.BlobName, parent);

await FileRepository.UpdateAsync(file, true, cancellationToken);

Expand Down Expand Up @@ -173,8 +173,8 @@ await CheckFileNotExistAsync(model.NewFileName, newParent?.Id, file.FileContaine
await CheckNotMovingDirectoryToSubDirectoryAsync(file, newParent);
}

file.UpdateInfo(model.NewFileName, file.MimeType, file.SubFilesQuantity, file.ByteSize, file.Hash,
file.BlobName, newParent);
file.UpdateInfo(model.NewFileName, file.MimeType, file.SubFilesQuantity, file.HasSubdirectories, file.ByteSize,
file.Hash, file.BlobName, newParent);

if (oldParent is not null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
namespace EasyAbp.FileManagement.Files
namespace EasyAbp.FileManagement.Files;

public class SubFilesStatisticDataModel
{
public class SubFilesStatisticDataModel
public int SubFilesQuantity { get; set; }

public bool HasSubdirectories { get; set; }

public long ByteSize { get; set; }

public SubFilesStatisticDataModel(int subFilesQuantity, bool hasSubdirectories, long byteSize)
{
public int SubFilesQuantity { get; set; }

public long ByteSize { get; set; }
SubFilesQuantity = subFilesQuantity;
HasSubdirectories = hasSubdirectories;
ByteSize = byteSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ await CheckFileNotExistAsync(
model.FileName, model.Parent?.Id, model.FileContainerName, model.OwnerUserId, false);

var file = new File(GuidGenerator.Create(), CurrentTenant.Id, model.Parent, model.FileContainerName,
model.FileName, model.MimeType, model.FileType, 0, model.GetContentLength(), hashString,
blobName, model.OwnerUserId);
model.FileName, model.MimeType, model.FileType, model.GetContentLength(), hashString, blobName,
model.OwnerUserId);

model.MapExtraPropertiesTo(file, MappingPropertyDefinitionChecks.Destination);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public virtual async Task<SubFilesStatisticDataModel> GetSubFilesStatisticDataAs
CancellationToken cancellationToken = default)
{
return await (await GetQueryableAsync()).Where(x => x.ParentId == id).GroupBy(x => true).Select(x =>
new SubFilesStatisticDataModel
{
SubFilesQuantity = x.Count(),
ByteSize = x.Sum(y => y.ByteSize)
}).FirstOrDefaultAsync(cancellationToken) ?? new SubFilesStatisticDataModel();
new SubFilesStatisticDataModel(
x.Count(),
x.Any(y => y.FileType == FileType.Directory),
x.Sum(y => y.ByteSize)
)).FirstOrDefaultAsync(cancellationToken) ?? new SubFilesStatisticDataModel(0, false, 0);
}

public virtual async Task<string> GetFileNameWithNextSerialNumberAsync(string fileName, Guid? parentId,
Expand Down
40 changes: 31 additions & 9 deletions test/EasyAbp.FileManagement.Domain.Tests/Files/FileDomainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,27 @@ public async Task Should_Update_Statistic_Data()
dir2.ShouldNotBeNull();

dir1.SubFilesQuantity.ShouldBe(2);
dir1.HasSubdirectories.ShouldBeTrue();
dir1.ByteSize.ShouldBe(7 + 8);
dir2.SubFilesQuantity.ShouldBe(2);
dir2.HasSubdirectories.ShouldBeTrue();
dir2.ByteSize.ShouldBe(0);

var dirs1 = await FileRepository.GetListAsync(dir1.Id, "test", null, FileType.Directory);
var subdirs1 = await FileRepository.GetListAsync(dir1.Id, "test", null, FileType.Directory);

dirs1.Count.ShouldBe(2);
subdirs1.Count.ShouldBe(2);

var dir11 = dirs1.FirstOrDefault(x => x.FileName == "dir11");
var dir12 = dirs1.FirstOrDefault(x => x.FileName == "dir12");
var dir11 = subdirs1.FirstOrDefault(x => x.FileName == "dir11");
var dir12 = subdirs1.FirstOrDefault(x => x.FileName == "dir12");

dir11.ShouldNotBeNull();
dir12.ShouldNotBeNull();

dir11.SubFilesQuantity.ShouldBe(1);
dir11.HasSubdirectories.ShouldBeFalse();
dir11.ByteSize.ShouldBe(7);
dir12.SubFilesQuantity.ShouldBe(1);
dir11.HasSubdirectories.ShouldBeFalse();
dir12.ByteSize.ShouldBe(8);

var files11 = await FileRepository.GetListAsync(dir11.Id, "test", null);
Expand All @@ -64,14 +68,17 @@ await WithUnitOfWorkAsync(async () =>

dir1 = await FileRepository.GetAsync(dir1.Id);
dir1.SubFilesQuantity.ShouldBe(2);
dir1.HasSubdirectories.ShouldBeTrue();
dir1.ByteSize.ShouldBe(8);

dir11 = await FileRepository.GetAsync(dir11.Id);
dir11.SubFilesQuantity.ShouldBe(0);
dir11.HasSubdirectories.ShouldBeFalse();
dir11.ByteSize.ShouldBe(0);

dir2 = await FileRepository.GetAsync(dir2.Id);
dir2.SubFilesQuantity.ShouldBe(3);
dir2.HasSubdirectories.ShouldBeTrue();
dir2.ByteSize.ShouldBe(7);

Guid newFileId = default;
Expand All @@ -86,10 +93,12 @@ await WithUnitOfWorkAsync(async () =>

dir1 = await FileRepository.GetAsync(dir1.Id);
dir1.SubFilesQuantity.ShouldBe(2);
dir1.HasSubdirectories.ShouldBeTrue();
dir1.ByteSize.ShouldBe(8 + 11);

dir12 = await FileRepository.GetAsync(dir12.Id);
dir12.SubFilesQuantity.ShouldBe(2);
dir12.HasSubdirectories.ShouldBeFalse();
dir12.ByteSize.ShouldBe(8 + 11);

await WithUnitOfWorkAsync(async () =>
Expand All @@ -100,10 +109,23 @@ await WithUnitOfWorkAsync(async () =>

dir1 = await FileRepository.GetAsync(dir1.Id);
dir1.SubFilesQuantity.ShouldBe(2);
dir1.HasSubdirectories.ShouldBeTrue();
dir1.ByteSize.ShouldBe(8);

dir12 = await FileRepository.GetAsync(dir12.Id);
dir12.SubFilesQuantity.ShouldBe(1);
dir12.HasSubdirectories.ShouldBeFalse();
dir12.ByteSize.ShouldBe(8);

await WithUnitOfWorkAsync(async () =>
{
await FileManager.CreateAsync(new CreateFileModel("test", null, "new-dir", null,
FileType.Directory, dir12, null));
});

dir12 = await FileRepository.GetAsync(dir12.Id);
dir12.SubFilesQuantity.ShouldBe(2);
dir12.HasSubdirectories.ShouldBeTrue();
dir12.ByteSize.ShouldBe(8);
}

Expand All @@ -120,20 +142,20 @@ public async Task Should_SoftDeletionToken_Work()

// since SQLite doesn't support unique index with null values.
var parent = await FileRepository.InsertAsync(new File(Guid.NewGuid(), tenantId, null,
"test", "parent", null, FileType.Directory, 0, 0, null, null, ownerUserId), true);
"test", "parent", null, FileType.Directory, 0, null, null, ownerUserId), true);

var dir = await FileRepository.InsertAsync(new File(Guid.NewGuid(), tenantId, parent,
"test", "dir", null, FileType.Directory, 0, 0, null, null, ownerUserId), true);
"test", "dir", null, FileType.Directory, 0, null, null, ownerUserId), true);

await Should.ThrowAsync<DbUpdateException>(() =>
FileRepository.InsertAsync(new File(Guid.NewGuid(), tenantId, parent,
"test", "dir", null, FileType.Directory, 0, 0, null, null, ownerUserId), true));
"test", "dir", null, FileType.Directory, 0, null, null, ownerUserId), true));

await FileManager.DeleteAsync(dir);

await Should.NotThrowAsync(() =>
FileRepository.InsertAsync(new File(Guid.NewGuid(), tenantId, parent,
"test", "dir", null, FileType.Directory, 0, 0, null, null, ownerUserId), true));
"test", "dir", null, FileType.Directory, 0, null, null, ownerUserId), true));

using (softDeleteFilter.Disable())
{
Expand All @@ -150,7 +172,7 @@ public async Task Should_New_File_Have_LastModificationTime_Value()
var dirId = Guid.NewGuid();

await WithUnitOfWorkAsync(() => FileRepository.InsertAsync(new File(dirId, null, null,
"test", "dir", null, FileType.Directory, 0, 0, null, null, null), true));
"test", "dir", null, FileType.Directory, 0, null, null, null), true));

var dir = await FileRepository.GetAsync(dirId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ public FileManagementDataSeedContributor(IFileManager fileManager)

public async Task SeedAsync(DataSeedContext context)
{
/* Instead of returning the Task.CompletedTask, you can insert your test data
* at this point!
/*
* Directories:
* dir1/ dir1/dir11 dir1/dir12
* dir2/ dir2/dir21 dir2/dir22
*
* Files:
* dir1/dir11/file1.txt dir1/dir12/file2.txt
*/

var dir1 = await _fileManager.CreateAsync(
Expand Down