Skip to content

Commit e85c109

Browse files
Refactor file reading to use async methods
1 parent 47934d9 commit e85c109

File tree

5 files changed

+98
-99
lines changed

5 files changed

+98
-99
lines changed

src/Core/Grand.Data/LiteDb/LiteDBStoreFilesContext.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Grand.Domain;
22
using LiteDB;
3-
using System.Xml.Linq;
43

54
namespace Grand.Data.LiteDb;
65

@@ -23,8 +22,8 @@ public async Task<byte[]> BucketDownload(string id)
2322
using (var stream = file.OpenRead())
2423
using (MemoryStream mstream = new())
2524
{
26-
stream.CopyTo(mstream);
27-
return await Task.FromResult(mstream.ToArray());
25+
await stream.CopyToAsync(mstream);
26+
return mstream.ToArray();
2827
}
2928
}
3029

src/Modules/Grand.Module.Installer/Services/InstallDataBrands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected virtual async Task InstallBrands()
2727
DisplayOrder = 1
2828
};
2929
brandXiaomi.PictureId = (await _pictureRepository.InsertPicture(
30-
File.ReadAllBytes(sampleImagesPath + "brand_xiaomi.jpg"), "image/pjpeg",
30+
await File.ReadAllBytesAsync(sampleImagesPath + "brand_xiaomi.jpg"), "image/pjpeg",
3131
"Xiaomi", "", "", false, Reference.Brand, brandXiaomi.Id)).Id;
3232
await _brandRepository.InsertAsync(brandXiaomi);
3333
allbrands.Add(brandXiaomi);
@@ -43,7 +43,7 @@ protected virtual async Task InstallBrands()
4343
DisplayOrder = 5
4444
};
4545
brandDell.PictureId = (await _pictureRepository.InsertPicture(
46-
File.ReadAllBytes(sampleImagesPath + "brand_dell.jpg"), "image/pjpeg",
46+
await File.ReadAllBytesAsync(sampleImagesPath + "brand_dell.jpg"), "image/pjpeg",
4747
("Dell"), "", "", false, Reference.Brand, brandDell.Id)).Id;
4848

4949
await _brandRepository.InsertAsync(brandDell);
@@ -60,7 +60,7 @@ protected virtual async Task InstallBrands()
6060
DisplayOrder = 5
6161
};
6262
brandAdidas.PictureId = (await _pictureRepository.InsertPicture(
63-
File.ReadAllBytes(sampleImagesPath + "brand_adidas.jpg"), "image/pjpeg",
63+
await File.ReadAllBytesAsync(sampleImagesPath + "brand_adidas.jpg"), "image/pjpeg",
6464
("Adidas"), "", "", false, Reference.Brand, brandAdidas.Id)).Id;
6565
await _brandRepository.InsertAsync(brandAdidas);
6666
allbrands.Add(brandAdidas);

src/Modules/Grand.Module.Installer/Services/InstallDataCategories.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected virtual async Task InstallCategories()
3535
DisplayOrder = 100
3636
};
3737
categoryComputers.PictureId = (await _pictureRepository.InsertPicture(
38-
File.ReadAllBytes(sampleImagesPath + "category_computers.jpeg"), "image/jpeg",
38+
await File.ReadAllBytesAsync(sampleImagesPath + "category_computers.jpeg"), "image/jpeg",
3939
"Computers", reference: Reference.Category,
4040
objectId: categoryComputers.Id)).Id;
4141
allCategories.Add(categoryComputers);
@@ -52,7 +52,7 @@ protected virtual async Task InstallCategories()
5252
DisplayOrder = categoryComputers.DisplayOrder + 1
5353
};
5454
categoryTablets.PictureId = (await _pictureRepository.InsertPicture(
55-
File.ReadAllBytes(sampleImagesPath + "category_tablets.jpg"), "image/jpeg",
55+
await File.ReadAllBytesAsync(sampleImagesPath + "category_tablets.jpg"), "image/jpeg",
5656
"Tablets", reference: Reference.Category,
5757
objectId: categoryTablets.Id))
5858
.Id;
@@ -70,7 +70,7 @@ protected virtual async Task InstallCategories()
7070
DisplayOrder = categoryComputers.DisplayOrder + 2
7171
};
7272
categoryNotebooks.PictureId = (await _pictureRepository.InsertPicture(
73-
File.ReadAllBytes(sampleImagesPath + "category_notebooks.jpg"), "image/jpeg",
73+
await File.ReadAllBytesAsync(sampleImagesPath + "category_notebooks.jpg"), "image/jpeg",
7474
"Notebooks", reference: Reference.Category,
7575
objectId: categoryNotebooks.Id)).Id;
7676
allCategories.Add(categoryNotebooks);
@@ -87,7 +87,7 @@ protected virtual async Task InstallCategories()
8787
DisplayOrder = categoryComputers.DisplayOrder + 3
8888
};
8989
categorySmartwatches.PictureId = (await _pictureRepository.InsertPicture(
90-
File.ReadAllBytes(sampleImagesPath + "category_smartwatches.jpg"), "image/jpeg",
90+
await File.ReadAllBytesAsync(sampleImagesPath + "category_smartwatches.jpg"), "image/jpeg",
9191
"Smartwatches", reference: Reference.Category,
9292
objectId: categorySmartwatches.Id)).Id;
9393
allCategories.Add(categorySmartwatches);
@@ -105,7 +105,7 @@ protected virtual async Task InstallCategories()
105105
DisplayOrder = 200
106106
};
107107
categoryElectronics.PictureId = (await _pictureRepository.InsertPicture(
108-
File.ReadAllBytes(sampleImagesPath + "category_electronics.jpeg"), "image/jpeg",
108+
await File.ReadAllBytesAsync(sampleImagesPath + "category_electronics.jpeg"), "image/jpeg",
109109
"Electronics", reference: Reference.Category,
110110
objectId: categoryElectronics.Id)).Id;
111111
allCategories.Add(categoryElectronics);
@@ -122,7 +122,7 @@ protected virtual async Task InstallCategories()
122122
DisplayOrder = categoryElectronics.DisplayOrder + 1
123123
};
124124
categoryDisplay.PictureId = (await _pictureRepository.InsertPicture(
125-
File.ReadAllBytes(sampleImagesPath + "category_display.jpeg"), "image/jpeg",
125+
await File.ReadAllBytesAsync(sampleImagesPath + "category_display.jpeg"), "image/jpeg",
126126
"Display", reference: Reference.Category,
127127
objectId: categoryDisplay.Id))
128128
.Id;
@@ -140,7 +140,7 @@ protected virtual async Task InstallCategories()
140140
DisplayOrder = categoryElectronics.DisplayOrder + 2
141141
};
142142
categorySmartphones.PictureId = (await _pictureRepository.InsertPicture(
143-
File.ReadAllBytes(sampleImagesPath + "category_smartphones.jpeg"), "image/jpeg",
143+
await File.ReadAllBytesAsync(sampleImagesPath + "category_smartphones.jpeg"), "image/jpeg",
144144
"Smartphones", reference: Reference.Category,
145145
objectId: categorySmartphones.Id)).Id;
146146
allCategories.Add(categorySmartphones);
@@ -158,7 +158,7 @@ protected virtual async Task InstallCategories()
158158
DisplayOrder = categoryElectronics.DisplayOrder + 3
159159
};
160160
categoryOthers.PictureId = (await _pictureRepository.InsertPicture(
161-
File.ReadAllBytes(sampleImagesPath + "category_accessories.jpg"), "image/jpeg",
161+
await File.ReadAllBytesAsync(sampleImagesPath + "category_accessories.jpg"), "image/jpeg",
162162
"SmAccessoriesartphones", reference: Reference.Category,
163163
objectId: categoryOthers.Id)).Id;
164164

@@ -177,7 +177,7 @@ protected virtual async Task InstallCategories()
177177
DisplayOrder = 300
178178
};
179179
categorySport.PictureId = (await _pictureRepository.InsertPicture(
180-
File.ReadAllBytes(sampleImagesPath + "category_sport.jpeg"), "image/jpeg",
180+
await File.ReadAllBytesAsync(sampleImagesPath + "category_sport.jpeg"), "image/jpeg",
181181
("Sport"), reference: Reference.Category, objectId: categorySport.Id)).Id;
182182
allCategories.Add(categorySport);
183183

@@ -193,7 +193,7 @@ protected virtual async Task InstallCategories()
193193
DisplayOrder = categorySport.DisplayOrder + 1
194194
};
195195
categoryShoes.PictureId = (await _pictureRepository.InsertPicture(
196-
File.ReadAllBytes(sampleImagesPath + "category_shoes.jpeg"), "image/jpeg",
196+
await File.ReadAllBytesAsync(sampleImagesPath + "category_shoes.jpeg"), "image/jpeg",
197197
"Shoes", reference: Reference.Category, objectId: categoryShoes.Id)).Id;
198198
allCategories.Add(categoryShoes);
199199

@@ -209,7 +209,7 @@ protected virtual async Task InstallCategories()
209209
DisplayOrder = categorySport.DisplayOrder + 2
210210
};
211211
categoryApparel.PictureId = (await _pictureRepository.InsertPicture(
212-
File.ReadAllBytes(sampleImagesPath + "category_sport.jpeg"), "image/jpeg",
212+
await File.ReadAllBytesAsync(sampleImagesPath + "category_sport.jpeg"), "image/jpeg",
213213
"Apparel", reference: Reference.Category,
214214
objectId: categoryApparel.Id))
215215
.Id;
@@ -228,7 +228,7 @@ protected virtual async Task InstallCategories()
228228
DisplayOrder = categorySport.DisplayOrder + 3
229229
};
230230
categoryBalls.PictureId = (await _pictureRepository.InsertPicture(
231-
File.ReadAllBytes(sampleImagesPath + "category_balls.jpeg"), "image/jpeg",
231+
await File.ReadAllBytesAsync(sampleImagesPath + "category_balls.jpeg"), "image/jpeg",
232232
("Balls"), reference: Reference.Category, objectId: categoryBalls.Id)).Id;
233233
allCategories.Add(categoryBalls);
234234

@@ -245,7 +245,7 @@ protected virtual async Task InstallCategories()
245245
DisplayOrder = 400
246246
};
247247
categoryDigitalDownloads.PictureId = (await _pictureRepository.InsertPicture(
248-
File.ReadAllBytes(sampleImagesPath + "category_digital_downloads.jpeg"), "image/jpeg",
248+
await File.ReadAllBytesAsync(sampleImagesPath + "category_digital_downloads.jpeg"), "image/jpeg",
249249
("Digital downloads"), reference: Reference.Category,
250250
objectId: categoryDigitalDownloads.Id)).Id;
251251
allCategories.Add(categoryDigitalDownloads);
@@ -267,7 +267,7 @@ protected virtual async Task InstallCategories()
267267
FlagStyle = "bg-success"
268268
};
269269
categoryLego.PictureId = (await _pictureRepository.InsertPicture(
270-
File.ReadAllBytes(sampleImagesPath + "category_lego.jpeg"), "image/jpeg",
270+
await File.ReadAllBytesAsync(sampleImagesPath + "category_lego.jpeg"), "image/jpeg",
271271
(categoryLego.Name), reference: Reference.Category,
272272
objectId: categoryLego.Id)).Id;
273273
allCategories.Add(categoryLego);
@@ -284,7 +284,7 @@ protected virtual async Task InstallCategories()
284284
DisplayOrder = 600
285285
};
286286
categoryGiftVouchers.PictureId = (await _pictureRepository.InsertPicture(
287-
File.ReadAllBytes(sampleImagesPath + "category_gift_cards.jpeg"), "image/jpeg",
287+
await File.ReadAllBytesAsync(sampleImagesPath + "category_gift_cards.jpeg"), "image/jpeg",
288288
(categoryGiftVouchers.Name), reference: Reference.Category,
289289
objectId: categoryGiftVouchers.Id)).Id;
290290
allCategories.Add(categoryGiftVouchers);

0 commit comments

Comments
 (0)