Skip to content

Commit e459762

Browse files
committed
Refactoring asset storage
1 parent d94a8b0 commit e459762

File tree

11 files changed

+22
-15
lines changed

11 files changed

+22
-15
lines changed

src/DragonFly.API.Core/ClientAPI/AssetApiStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public async Task<Result<QueryResult<Asset>>> QueryAsync(AssetQuery assetQuery)
3030
return result.ToResult(x => x.Convert(e => e.ToModel()));
3131
}
3232

33-
public async Task<Result> UploadAsync(Asset asset, string mimetype, Stream stream)
33+
public async Task<Result> UploadAsync(Guid assetId, string mimetype, Stream stream)
3434
{
35-
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, $"api/asset/{asset.Id}/upload");
35+
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, $"api/asset/{assetId}/upload");
3636
requestMessage.Content = new StreamContent(stream);
3737
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(mimetype);
3838

src/DragonFly.API/Rest/AssetApiExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private static async Task<IResult> MapUpload(HttpContext context, IAssetStorage
123123
return TypedResults.NotFound();
124124
}
125125

126-
return (await storage.UploadAsync(asset, context.Request.ContentType, context.Request.Body)).ToHttpResult();
126+
return (await storage.UploadAsync(asset.Id, context.Request.ContentType, context.Request.Body)).ToHttpResult();
127127
}
128128

129129
private static async Task<IResult> MapRefreshMetadata(IAssetStorage storage, Guid id)

src/DragonFly.AspNetCore/Permissions/Storages/AssetPermissionStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public async Task<Result> UpdateAsync(Asset asset)
7676
return await Api.AuthorizeAsync(PrincipalContext.Current, AssetPermissions.UpdateAsset).ThenAsync(x => Storage.UpdateAsync(asset));
7777
}
7878

79-
public async Task<Result> UploadAsync(Asset asset, string mimetype, Stream stream)
79+
public async Task<Result> UploadAsync(Guid assetId, string mimetype, Stream stream)
8080
{
81-
return await Api.AuthorizeAsync(PrincipalContext.Current, AssetPermissions.UploadAsset).ThenAsync(x => Storage.UploadAsync(asset, mimetype, stream));
81+
return await Api.AuthorizeAsync(PrincipalContext.Current, AssetPermissions.UploadAsset).ThenAsync(x => Storage.UploadAsync(assetId, mimetype, stream));
8282
}
8383
}

src/DragonFly.Client/Pages/Assets/AssetDetail.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
</div>
140140
@if (Entity.Hash != null)
141141
{
142-
<div style="margin: 1rem 0;">
142+
<div class="image-preview">
143143
@AssetPreviewManager.CreateComponent(Entity)
144144
</div>
145145
}

src/DragonFly.Client/Pages/Assets/AssetDetail.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected async Task OnInputFileChange(InputFileChangeEventArgs e)
105105

106106
using (Stream stream = SelectedFile.OpenReadStream(long.MaxValue))
107107
{
108-
await AssetService.UploadAsync(Entity, SelectedFile.ContentType, stream);
108+
await AssetService.UploadAsync(Entity.Id, SelectedFile.ContentType, stream);
109109
}
110110

111111
await RefreshAsync();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.image-preview {
2+
margin: 1rem 0;
3+
display: flex;
4+
justify-content: center;
5+
}

src/DragonFly.Client/Shared/AssetSelector.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
@if (Asset != null && ShowPreview)
4141
{
42-
<div class="image" style="margin: 1rem 0;">
42+
<div class="image-preview">
4343
@if (string.IsNullOrEmpty(Asset.PreviewUrl) == false)
4444
{
4545
@if (Asset.IsSVG())

src/DragonFly.Client/Shared/AssetSelector.razor.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
height: 150px;
66
}
77

8-
.image {
8+
.image-preview {
99
margin: 1rem 0;
1010
display: flex;
1111
justify-content: center;

src/DragonFly.Core/Modules/Assets/IAssetStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface IAssetStorage
2323

2424
Task<Result> PublishAsync(Asset asset);
2525

26-
Task<Result> UploadAsync(Asset asset, string mimetype, Stream stream);
26+
Task<Result> UploadAsync(Guid assetId, string mimetype, Stream stream);
2727

2828
Task<Result<Stream>> GetStreamAsync(Asset asset);
2929

src/DragonFly.MongoDB/Storages/AssetMongoStorage.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,21 @@ await Assets.UpdateOneAsync(
128128
return Result.Ok();
129129
}
130130

131-
public async Task<Result> UploadAsync(Asset asset, string mimetype, Stream stream)
131+
public async Task<Result> UploadAsync(Guid assetId, string mimetype, Stream stream)
132132
{
133133
//upload new stream to asset
134-
await AssetData.UploadFromStreamAsync(asset.Id.ToString(), stream);
134+
await AssetData.UploadFromStreamAsync(assetId.ToString(), stream);
135+
136+
Asset? asset = null;
135137

136138
//refresh asset info
137-
using (Stream s = await AssetData.OpenDownloadStreamByNameAsync(asset.Id.ToString()))
139+
using (Stream s = await AssetData.OpenDownloadStreamByNameAsync(assetId.ToString()))
138140
{
139141
byte[] hash = await SHA256.HashDataAsync(s);
140142

141143
string hashString = Convert.ToHexString(hash).ToLowerInvariant();
142144

143-
var mongoAsset = await Assets.FindOneAndUpdateAsync(Builders<MongoAsset>.Filter.Eq(x => x.Id, asset.Id),
145+
var mongoAsset = await Assets.FindOneAndUpdateAsync(Builders<MongoAsset>.Filter.Eq(x => x.Id, assetId),
144146
Builders<MongoAsset>.Update
145147
.Set(x => x.Size, s.Length)
146148
.Set(x => x.Hash, hashString)

0 commit comments

Comments
 (0)