Skip to content

Commit 7222d12

Browse files
committed
refactor: 更新 EditorUploadFile 类
1 parent 8bc2ab4 commit 7222d12

1 file changed

Lines changed: 12 additions & 91 deletions

File tree

src/components/BootstrapBlazor.SummerNote/Components/Editor/EditorUploadFile.cs

Lines changed: 12 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,111 +5,32 @@
55
namespace BootstrapBlazor.Components;
66

77
/// <summary>
8-
/// 文件信息
8+
/// Editor 上传文件信息类
99
/// </summary>
10-
public class EditorUploadFile
10+
public class EditorUploadFile(string fileName, string contentType, long fileSize, Stream stream)
1111
{
1212
/// <summary>
13-
/// 文件名
13+
/// 获得/设置 文件名
1414
/// </summary>
15-
public string? FileName { get; set; }
15+
public string FileName => fileName;
1616

1717
/// <summary>
18-
/// 文件大小
18+
/// 获得/设置 文件大小
1919
/// </summary>
20-
public long FileSize { get; set; }
20+
public long FileSize => fileSize;
2121

2222
/// <summary>
23-
/// 最后修改日期
23+
/// 获得/设置 文件类型
2424
/// </summary>
25-
public string? LastModified { get; set; }
25+
public string ContentType => contentType;
2626

2727
/// <summary>
28-
/// 文件类型
28+
/// 获得/设置 上传的文件流
2929
/// </summary>
30-
public string? ContentType { get; set; }
30+
public Stream Stream => stream;
3131

3232
/// <summary>
33-
/// 上传的文件流
33+
/// 获得/设置 错误信息
3434
/// </summary>
35-
public Stream? UploadStream { get; set; }
36-
37-
/// <summary>
38-
/// 文件索引
39-
/// <para>用于标识文件在上传列表中的位置</para>
40-
/// <para>例如在上传多个文件时可以通过此属性区分每个文件的索引位置</para>
41-
/// </summary>
42-
public int Index { get; set; }
43-
44-
/// <summary>
45-
/// 返回码,0为成功,非0失败
46-
/// </summary>
47-
public int Code { get; set; }
48-
49-
/// <summary>
50-
/// 错误信息
51-
/// </summary>
52-
public string? Error { get; set; }
53-
54-
/// <summary>
55-
/// 保存到文件
56-
/// </summary>
57-
/// <param name="fileName"></param>
58-
/// <param name="token"></param>
59-
/// <returns></returns>
60-
public async Task<bool> SaveToFile(string fileName, CancellationToken token = default)
61-
{
62-
var ret = false;
63-
if (UploadStream != null)
64-
{
65-
// 文件保护,如果文件存在则先删除
66-
if (System.IO.File.Exists(fileName))
67-
{
68-
try
69-
{
70-
System.IO.File.Delete(fileName);
71-
}
72-
catch (Exception ex)
73-
{
74-
Code = 1002;
75-
Error = ex.Message;
76-
}
77-
}
78-
79-
var folder = Path.GetDirectoryName(fileName);
80-
if (!string.IsNullOrEmpty(folder) && !Directory.Exists(folder))
81-
{
82-
Directory.CreateDirectory(folder);
83-
}
84-
85-
if (Code == 0)
86-
{
87-
using var uploadFile = File.OpenWrite(fileName);
88-
89-
try
90-
{
91-
// 打开文件流
92-
var stream = UploadStream;
93-
94-
var buffer = new byte[4 * 1096];
95-
int bytesRead = 0;
96-
97-
// 开始读取文件
98-
while ((bytesRead = await stream.ReadAsync(buffer, token)) > 0)
99-
{
100-
await uploadFile.WriteAsync(buffer.AsMemory(0, bytesRead), token);
101-
102-
}
103-
ret = true;
104-
}
105-
catch (Exception ex)
106-
{
107-
Code = 1003;
108-
Error = ex.Message;
109-
}
110-
}
111-
}
112-
return ret;
113-
}
114-
35+
public Exception? Error { get; set; }
11536
}

0 commit comments

Comments
 (0)