10/23更新

This commit is contained in:
橙子
2022-10-23 22:29:35 +08:00
parent 16d25fb60d
commit 5da2143212
8 changed files with 54 additions and 18 deletions

View File

@@ -134,19 +134,34 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{ {
await f.CopyToAsync(stream); await f.CopyToAsync(stream);
//如果是图片类型,还需要生成缩略图 //如果是图片类型,还需要生成缩略图,当然,如果图片很小,直接复制过去即可
if (PathEnum.Image.ToString().Equals(type)) if (PathEnum.Image.ToString().Equals(type))
{ {
//保存至缩略图路径
var result = _thumbnailSharpInvoer.CreateThumbnailBytes(thumbnailSize: 300,
imageStream: stream,
imageFormat: Format.Jpeg);
string thumbnailPath = $"{PathConst.wwwroot}/{PathEnum.Thumbnail}"; string thumbnailPath = $"{PathConst.wwwroot}/{PathEnum.Thumbnail}";
if (!Directory.Exists(thumbnailPath)) if (!Directory.Exists(thumbnailPath))
{ {
Directory.CreateDirectory(thumbnailPath); Directory.CreateDirectory(thumbnailPath);
} }
await System.IO.File.WriteAllBytesAsync(Path.Combine(thumbnailPath, filename), result); //保存至缩略图路径
byte[] result=null!;
try
{
result = _thumbnailSharpInvoer.CreateThumbnailBytes(thumbnailSize: 300,
imageStream: stream,
imageFormat: Format.Jpeg);
}
catch
{
result = new byte[stream.Length];
stream.Read(result, 0, result.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
}
finally
{
await System.IO.File.WriteAllBytesAsync(Path.Combine(thumbnailPath, filename), result);
}
} }
@@ -173,7 +188,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{ {
string typePath = $"{PathConst.wwwroot}/{PathEnum.Image}"; string typePath = $"{PathConst.wwwroot}/{PathEnum.Image}";
string thumbnailPath = $"{PathConst.wwwroot}/{PathEnum.Thumbnail}"; string thumbnailPath = $"{PathConst.wwwroot}/{PathEnum.Thumbnail}";
List<string> fileNames =FileHelper. GetAllFileNames(typePath); List<string> fileNames = FileHelper.GetAllFileNames(typePath);
foreach (var filename in fileNames) foreach (var filename in fileNames)
{ {
if (System.IO.File.Exists(Path.Combine(thumbnailPath, filename))) if (System.IO.File.Exists(Path.Combine(thumbnailPath, filename)))
@@ -185,20 +200,41 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{ {
Directory.CreateDirectory(typePath); Directory.CreateDirectory(typePath);
} }
using (var stream = new FileStream(Path.Combine(typePath, filename), FileMode.Open, FileAccess.ReadWrite)) using (var stream = new FileStream(Path.Combine(typePath, filename), FileMode.Open, FileAccess.ReadWrite))
{ {
//保存至缩略图路径 byte[] result=null!;
var result = _thumbnailSharpInvoer.CreateThumbnailBytes(thumbnailSize: 300, try
imageStream: stream,
imageFormat: Format.Jpeg);
if (!Directory.Exists(thumbnailPath))
{ {
Directory.CreateDirectory(thumbnailPath);
}
await System.IO.File.WriteAllBytesAsync(Path.Combine(thumbnailPath, filename), result);
};
//保存至缩略图路径
result = _thumbnailSharpInvoer.CreateThumbnailBytes(thumbnailSize: 300,
imageStream: stream,
imageFormat: Format.Jpeg);
if (!Directory.Exists(thumbnailPath))
{
Directory.CreateDirectory(thumbnailPath);
}
}
catch
{
result = new byte[stream.Length];
stream.Read(result, 0, result.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
////如果当前文件同步失败,就跳转到下一个
}
finally {
await System.IO.File.WriteAllBytesAsync(Path.Combine(thumbnailPath, filename), result);
}
}
} }
return Result.Success(); return Result.Success();
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 153 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.4 KiB

View File

@@ -3,7 +3,7 @@
round round
:width="width" :width="width"
:height="height" :height="height"
:src="url+(src??'0')+'/true'" :src="url+(src??'0')"
/> />
</template> </template>