Skip to content

Commit a24a920

Browse files
authored
feat: support random file name when upload file to oss (#770)
* feat: support select random file name when upload file to oss * chore: update
1 parent fdbdac3 commit a24a920

File tree

9 files changed

+75
-28
lines changed

9 files changed

+75
-28
lines changed

src/Masa.Stack.Components/Pages/UserCenters/UploadAvatar.razor.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ namespace Masa.Stack.Components;
55

66
public partial class UploadAvatar : SUploadImage
77
{
8+
[Parameter]
9+
public bool RandomName { get; set; }
10+
811
[Inject]
912
public IDccClient Client { get; set; } = default!;
1013

@@ -33,7 +36,17 @@ protected override async Task OnInputFileChange(InputFileChangeEventArgs e)
3336
public override async Task UploadAsync()
3437
{
3538
var response = await Client.OpenApiService.GetOssSecurityTokenAsync();
36-
OnInputFileUpload = FileUploadCallBack.CreateCallback("UploadImage", response);
39+
OnInputFileUpload = FileUploadCallBack.CreateCallback("UploadImage",
40+
new
41+
{
42+
response.AccessKeyId,
43+
response.AccessKeySecret,
44+
response.Region,
45+
response.Bucket,
46+
response.StsToken,
47+
response.CdnHost,
48+
RandomName
49+
});
3750
await base.UploadAsync();
3851
}
3952
}

src/Masa.Stack.Components/Pages/UserCenters/UserInfo.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<div class="user-info__left">
1010
<div class="avatar">
1111
<UploadAvatar Size="140"
12+
RandomName
1213
Class="mb-6"
1314
Value="@UpdateUserAvatar.Avatar"
1415
ValueChanged="UpdateAvatarAsync"

src/Masa.Stack.Components/wwwroot/js/oss/upload.js

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,70 @@ async function UploadImage(imageFiles, ossParamter) {
22
const client = new OSS(ossParamter);
33
let cdnHost = getCdnHost(ossParamter);
44
const headers = {
5-
// 指定该Object被下载时网页的缓存行为。
5+
// 指定该Object被下载时网页的缓存行为。
66
// 'Cache-Control': 'no-cache',
7-
// 指定该Object被下载时的名称。
7+
// 指定该Object被下载时的名称。
88
// 'Content-Disposition': 'oss_download.txt',
9-
// 指定该Object被下载时的内容编码格式。
9+
// 指定该Object被下载时的内容编码格式。
1010
// 'Content-Encoding': 'UTF-8',
11-
// 指定过期时间。
11+
// 指定过期时间。
1212
// 'Expires': 'Wed, 08 Jul 2022 16:57:01 GMT',
13-
// 指定Object的存储类型。
13+
// 指定Object的存储类型。
1414
// 'x-oss-storage-class': 'Standard',
15-
// 指定Object的访问权限。
15+
// 指定Object的访问权限。
1616
'x-oss-object-acl': 'public-read-write',
17-
// 设置Object的标签,可同时设置多个标签。
17+
// 设置Object的标签,可同时设置多个标签。
1818
// 'x-oss-tagging': 'Tag1=1&Tag2=2',
19-
// 指定CopyObject操作时是否覆盖同名目标Object。此处设置为true,表示禁止覆盖同名Object。
19+
// 指定CopyObject操作时是否覆盖同名目标Object。此处设置为true,表示禁止覆盖同名Object。
2020
// 'x-oss-forbid-overwrite': 'true',
2121
};
22-
return await putObject(client, imageFiles[0], headers, cdnHost);
22+
console.log(ossParamter);
23+
return await putObject(client, imageFiles[0], headers, cdnHost, ossParamter.randomName);
2324
}
2425

2526
function getCdnHost(ossParamter) {
26-
let cdnHost = ((ossParamter || {}).cdnHost || "").trim();
27+
let cdnHost = (ossParamter.cdnHost || "").trim();
2728
if (cdnHost.length == 0) {
2829
return cdnHost;
2930
}
3031
if (cdnHost[cdnHost.length - 1] == '/')
3132
return cdnHost.substring(0, cdnHost.length - 1);
3233
}
3334

34-
async function putObject(client, file, headers, cdnHost) {
35+
function getFileName(file, rand) {
36+
if (!rand) return file.name;
37+
let time = formatTime(new Date()), random = Math.random().toString().slice(3, 9);
38+
let lastPosition = file.name.lastIndexOf('.');
39+
let ext = lastPosition >= 0 ? file.name.slice(lastPosition) : '';
40+
return `${time}_${random}${ext}`;
41+
}
42+
43+
function formatTime(date) {
44+
const year = date.getFullYear();
45+
const month = (date.getMonth() + 1).toString().padStart(2, '0');
46+
const day = date.getDate().toString().padStart(2, '0');
47+
const hour = date.getHours().toString().padStart(2, '0');
48+
const minute = date.getMinutes().toString().padStart(2, '0');
49+
const second = date.getSeconds().toString().padStart(2, '0');
50+
const offset = -date.getTimezoneOffset();
51+
const sign = offset >= 0 ? 1 : 0;
52+
return `${year}${month}${day}${hour}${minute}${second}_${sign}${offset}`;
53+
}
54+
55+
async function putObject(client, file, headers, cdnHost, randName) {
3556
try {
36-
// 填写Object完整路径。Object完整路径中不能包含Bucket名称。
37-
// 您可以通过自定义文件名(例如exampleobject.txt)或文件完整路径(例如exampledir/exampleobject.txt)的形式实现将数据上传到当前Bucket或Bucket中的指定目录。
38-
// data对象可以自定义为file对象、Blob数据或者OSS Buffer。
57+
let fileName = getFileName(file, randName);
58+
// 填写Object完整路径。Object完整路径中不能包含Bucket名称。
59+
// 您可以通过自定义文件名(例如exampleobject.txt)或文件完整路径(例如exampledir/exampleobject.txt)的形式实现将数据上传到当前Bucket或Bucket中的指定目录。
60+
// data对象可以自定义为file对象、Blob数据或者OSS Buffer。
3961
const result = await client.put(
40-
`${file.name}`,
62+
`${fileName}`,
4163
file,
4264
{
4365
headers
4466
}
4567
);
4668
console.log(result);
47-
console.log("cdnHost: " + cdnHost);
4869
let url = cdnHost ? (cdnHost + "/" + result.name) : result.url;
4970
console.log(url)
5071
return [url];

tests/MasaWasmApp/MasaWasmApp.csproj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
1+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.*" />
12-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="6.0.*" />
13-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.*" PrivateAssets="all" />
11+
<PackageReference Include="Microsoft.AspNetCore.Components.CustomElements" Version="8.0.*" />
12+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.*" />
13+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.*" />
14+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.*" PrivateAssets="all" />
1415
</ItemGroup>
1516

1617
<ItemGroup>
1718
<ProjectReference Include="..\..\src\Masa.Stack.Components\Masa.Stack.Components.csproj" />
1819
</ItemGroup>
1920

2021
<ItemGroup>
22+
<Content Update="wwwroot\appsettings.Staging.json">
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
</Content>
2125
<Content Update="wwwroot\appsettings.Development.json">
2226
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2327
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>

tests/MasaWasmApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ await builder.AddMasaOpenIdConnectAsync(new MasaOpenIdConnectOptions
1414
Scopes = new List<string> { "openid", "profile" }
1515
});
1616

17-
builder.Services.AddMasaStackComponentsWithNormalApp(MasaStackProject.Auth, "http://localhost:4317", "1.0.0");
17+
builder.Services.AddMasaStackComponent(MasaStackProject.Auth, "", microFrontend: false);
1818
var host = builder.Build();
1919
await host.Services.InitializeMasaStackApplicationAsync();
2020
await host.RunAsync();

tests/MasaWasmApp/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"dotnetRunMessages": true,
2424
"launchBrowser": true,
2525
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
26-
"applicationUrl": "https://localhost:7017;http://localhost:5281",
26+
"applicationUrl": "https://localhost:18100",
2727
"environmentVariables": {
28-
"ASPNETCORE_ENVIRONMENT": "Development"
28+
"ASPNETCORE_ENVIRONMENT": "Staging"
2929
}
3030
},
3131
"IIS Express": {

tests/MasaWasmApp/wwwroot/appsettings.Development.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"MASA_VERSION": "1.0-Prev",
33
"MASA_CLUSTER": "Default",
44
"OTLP_URL": "https://otel-collector.masastack:9013",
5-
"MASA_ENVIRONMENT": "Develop",
6-
"MASA_STACK": "[{\"id\":\"pm\",\"service\":{\"id\":\"pm-service-dev\",\"domain\":\"https://pm-service-dev.masastack.com\"},\"web\":{\"id\":\"pm-web-dev\",\"domain\":\"https://pm-dev.masastack.com\"}},{\"id\":\"dcc\",\"service\":{\"id\":\"dcc-service-dev\",\"domain\":\"https://dcc-service-dev.masastack.com\"},\"web\":{\"id\":\"dcc-web-dev\",\"domain\":\"https://dcc-dev.masastack.com\"}},{\"id\":\"tsc\",\"service\":{\"id\":\"tsc-service-dev\",\"domain\":\"https://tsc-service-dev.masastack.com\"},\"web\":{\"id\":\"tsc-web-dev\",\"domain\":\"https://tsc-dev.masastack.com\"}},{\"id\":\"alert\",\"service\":{\"id\":\"alert-service-dev\",\"domain\":\"https://alert-service-dev.masastack.com\"},\"web\":{\"id\":\"alert-web-dev\",\"domain\":\"https://alert-dev.masastack.com\"}},{\"id\":\"scheduler\",\"service\":{\"id\":\"scheduler-service-dev\",\"domain\":\"https://scheduler-service-dev.masastack.com\"},\"worker\":{\"id\":\"scheduler-worker-dev\",\"domain\":\"https://scheduler-worker-dev.masastack.com\"},\"web\":{\"id\":\"scheduler-web-dev\",\"domain\":\"https://scheduler-dev.masastack.com\"}},{\"id\":\"mc\",\"service\":{\"id\":\"mc-service-dev\",\"domain\":\"https://mc-service-dev.masastack.com\"},\"web\":{\"id\":\"mc-web-dev\",\"domain\":\"https://mc-dev.masastack.com\"}},{\"id\":\"auth\",\"service\":{\"id\":\"auth-service-dev\",\"domain\":\"https://auth-service-dev.masastack.com\"},\"web\":{\"id\":\"auth-web-dev\",\"domain\":\"https://auth-dev.masastack.com\"},\"sso\":{\"id\":\"auth-sso-dev\",\"domain\":\"https://auth-sso-dev.masastack.com\"}}]"
5+
"MASA_ENVIRONMENT": "Staging",
6+
"MASA_STACK": "[{\n\t\"id\": \"pm\",\n\t\"name\": \"PM\",\n\t\"service\": {\n\t\t\"name\": \"PM.Service\",\n\t\t\"id\": \"pm-service-staging\",\n\t\t\"domain\": \"https://pm-service-sec-staging.masastack.com\"},\n\t\"web\": {\n\t\t\"name\": \"PM.Web\",\n\t\t\"id\": \"pm-web-staging\",\n\t\t\"domain\": \"https://pm-sec-staging.masastack.com\"\n\t}\n}, {\n\t\"id\": \"dcc\",\n\t\"name\": \"DCC\",\n\t\"service\": {\n\t\t\"name\": \"Dcc.Service\",\n\t\t\"id\": \"dcc-service-staging\",\n\t\t\"domain\": \"https://dcc-service-sec-staging.masastack.com\"\n\t},\n\t\"web\": {\n\t\t\"name\": \"DCC.Web\",\n\t\t\"id\": \"dcc-web-staging\",\n\t\t\"domain\": \"https://dcc-sec-staging.masastack.com\"\n\t}\n}, {\n\t\"id\": \"tsc\",\n\t\"name\": \"TSC\",\n\t\"service\": {\n\t\t\"name\": \"TSC.Service\",\n\t\t\"id\": \"tsc-service-staging\",\n\t\t\"domain\": \"https://tsc-service-sec-staging.masastack.com\"\n\t},\n\t\"web\": {\n\t\t\"name\": \"TSC.Web\",\n\t\t\"id\": \"tsc-web-staging\",\n\t\t\"domain\": \"https://tsc-sec-staging.masastack.com\"\n\t}\n}, {\n\t\"id\": \"alert\",\n\t\"name\": \"Alert\",\n\t\"service\": {\n\t\t\"name\": \"Alert.Service\",\n\t\t\"id\": \"alert-service-staging\",\n\t\t\"domain\": \"https://alert-service-sec-staging.masastack.com\"\n\t},\n\t\"web\": {\n\t\t\"name\": \"Alert.Web\",\n\t\t\"id\": \"alert-web-staging\",\n\t\t\"domain\": \"https://alert-sec-staging.masastack.com\"\n\t}\n}, {\n\t\"id\": \"scheduler\",\n\t\"name\": \"Scheduler\",\n\t\"service\": {\n\t\t\"name\": \"Scheduler.Service\",\n\t\t\"id\": \"scheduler-service-staging\",\n\t\t\"domain\": \"https://scheduler-service-sec-staging.masastack.com\"\n\t},\n\t\"worker\": {\n\t\t\"name\": \"Scheduler.Worker\",\n\t\t\"id\": \"scheduler-worker-staging\",\n\t\t\"domain\": \"https://scheduler-worker-sec-staging.masastack.com/\"\n\t},\n\t\"web\": {\n\t\t\"name\": \"Scheduler.Web\",\n\t\t\"id\": \"scheduler-web-staging\",\n\t\t\"domain\": \"https://scheduler-sec-staging.masastack.com\"\n\t}\n}, {\n\t\"id\": \"mc\",\n\t\"name\": \"MC\",\n\t\"service\": {\n\t\t\"name\": \"MC.Service\",\n\t\t\"id\": \"mc-service-staging\",\n\t\t\"domain\": \"https://mc-service-sec-staging.masastack.com\"\n\t},\n\t\"web\": {\n\t\t\"name\": \"MC.Web\",\n\t\t\"id\": \"mc-web-staging\",\n\t\t\"domain\": \"https://mc-sec-staging.masastack.com\"\n\t}\n}, {\n\t\"id\": \"auth\",\n\t\"name\": \"Auth\",\n\t\"service\": {\n\t\t\"name\": \"Auth.Service\",\n\t\t\"id\": \"auth-service-staging\",\n\t\t\"domain\": \"https://auth-service-sec-staging.masastack.com\"\n\t},\n\t\"web\": {\n\t\t\"name\": \"Auth.Web\",\n\t\t\"id\": \"auth-web-staging\",\n\t\t\"domain\": \"https://auth-sec-staging.masastack.com\"\n\t},\n\t\"sso\": {\n\t\t\"name\": \"Auth.SSO\",\n\t\t\"id\": \"auth-sso-staging\",\n\t\t\"domain\": \"https://auth-sso-sec-staging.masastack.com\"\n\t}\n}]"
77
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"MASA_VERSION": "1.0.0",
3+
"MASA_CLUSTER": "Default",
4+
"MASA_ENVIRONMENT": "Staging",
5+
"OTLP_URL": "https://otel-collector.masastack:9013",
6+
"MASA_STACK": "[{\"id\":\"pm\",\"name\":\"PM\",\"service\":{\"name\":\"PM.Service\",\"id\":\"pm-service-staging\",\"domain\":\"https://pm-service-sec-staging.masastack.com\"},\"web\":{\"name\":\"PM.Web\",\"id\":\"pm-web-staging\",\"domain\":\"https://pm-sec-staging.masastack.com\"}},{\"id\":\"dcc\",\"name\":\"DCC\",\"service\":{\"name\":\"Dcc.Service\",\"id\":\"dcc-service-staging\",\"domain\":\"https://dcc-service-sec-staging.masastack.com\"},\"web\":{\"name\":\"DCC.Web\",\"id\":\"dcc-web-staging\",\"domain\":\"https://dcc-sec-staging.masastack.com\"}},{\"id\":\"tsc\",\"name\":\"TSC\",\"service\":{\"name\":\"TSC.Service\",\"id\":\"tsc-service-staging\",\"domain\":\"https://tsc-service-sec-staging.masastack.com\"},\"web\":{\"name\":\"TSC.Web\",\"id\":\"tsc-web-staging\",\"domain\":\"https://tsc-sec-staging.masastack.com\"}},{\"id\":\"alert\",\"name\":\"Alert\",\"service\":{\"name\":\"Alert.Service\",\"id\":\"alert-service-staging\",\"domain\":\"https://alert-service-sec-staging.masastack.com\"},\"web\":{\"name\":\"Alert.Web\",\"id\":\"alert-web-staging\",\"domain\":\"https://alert-sec-staging.masastack.com\"}},{\"id\":\"scheduler\",\"name\":\"Scheduler\",\"service\":{\"name\":\"Scheduler.Service\",\"id\":\"scheduler-service-staging\",\"domain\":\"https://scheduler-service-sec-staging.masastack.com\"},\"worker\":{\"name\":\"Scheduler.Worker\",\"id\":\"scheduler-worker-staging\",\"domain\":\"https://scheduler-worker-sec-staging.masastack.com\"},\"web\":{\"name\":\"Scheduler.Web\",\"id\":\"scheduler-web-staging\",\"domain\":\"https://scheduler-sec-staging.masastack.com\"}},{\"id\":\"mc\",\"name\":\"MC\",\"service\":{\"name\":\"MC.Service\",\"id\":\"mc-service-staging\",\"domain\":\"https://mc-service-sec-staging.masastack.com\"},\"web\":{\"name\":\"MC.Web\",\"id\":\"mc-web-staging\",\"domain\":\"https://mc-sec-staging.masastack.com\"}},{\"id\":\"auth\",\"name\":\"Auth\",\"service\":{\"name\":\"Auth.Service\",\"id\":\"auth-service-staging\",\"domain\":\"https://auth-service-sec-staging.masastack.com\"},\"web\":{\"name\":\"Auth.Web\",\"id\":\"auth-web-staging\",\"domain\":\"https://auth-sec-staging.masastack.com\"},\"sso\":{\"name\":\"Auth.SSO\",\"id\":\"auth-sso-staging\",\"domain\":\"https://auth-sso-sec-staging.masastack.com\"}},{\"id\":\"superapp\",\"service\":{\"id\":\"superapp-service-staging\",\"domain\":\"https://sup-service-sec-staging.masastack.com\"},\"web\":{\"id\":\"superapp-web-staging\",\"domain\":\"https://sup-sec-staging.masastack.com\"}}]"
7+
}

tests/MasaWasmApp/wwwroot/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<link rel="icon" type="image/png" href="favicon.png" />
1111
<link href="MasaWasmApp.styles.css" rel="stylesheet" />
1212
<link href="_content/Masa.Blazor/css/masa-blazor.min.css" rel="stylesheet">
13+
<link href="_content/Masa.Stack.Components/css/standard.css" rel="stylesheet">
1314
<link href="_content/Masa.Stack.Components/css/app.css" rel="stylesheet">
1415
<link href="https://cdn.masastack.com/npm/@mdi/font@5.x/css/materialdesignicons.min.css" rel="stylesheet">
1516
<link href="https://cdn.masastack.com/npm/materialicons/materialicons.css" rel="stylesheet">
@@ -32,7 +33,7 @@
3233
<a href="" class="reload">Reload</a>
3334
<a class="dismiss">🗙</a>
3435
</div>
35-
<script src="_framework/blazor.webassembly.js"></script>
36+
<script src="_framework/blazor.webassembly.js"></script>
3637
<script src="_content/Masa.Blazor/js/masa-blazor.js"></script>
3738
<script src="_content/Masa.Stack.Components/js/components.js"></script>
3839
<script src="_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script>

0 commit comments

Comments
 (0)