Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(upload): make size-limit support object #1763

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,19 @@ export const calcIcon = (icon: string | Record<string, any>, defaultIcon?: strin
}
return null;
};

export const isOverSize = (size, sizeLimit) => {
if (!sizeLimit) return false;

const base = 1000;
const unitMap = {
B: 1,
KB: base,
MB: base * base,
GB: base * base * base,
};
const computedSize =
typeof sizeLimit === 'number' ? sizeLimit * base : sizeLimit?.size * unitMap[sizeLimit?.unit ?? 'KB']; // 单位 KB

return size > computedSize;
};
2 changes: 1 addition & 1 deletion src/upload/__test__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('upload', () => {
></t-upload>
`,
data: {
sizeLimit: 10240,
sizeLimit: 102,
},
methods: {
handleSelectChange,
Expand Down
23 changes: 19 additions & 4 deletions src/upload/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isObject, SuperComponent, wxComponent } from '../common/src/index';
import props from './props';
import { UploadFile } from './type';
import config from '../common/config';
import { isOverSize } from '../common/utils';

const { prefix } = config;
const name = `${prefix}-upload`;
Expand Down Expand Up @@ -196,10 +197,17 @@ export default class Upload extends SuperComponent {
// 支持单/多文件
res.tempFiles.forEach((temp) => {
const { size, fileType, tempFilePath, width, height, duration, thumbTempFilePath, ...res } = temp;
if (sizeLimit && size > sizeLimit) {
wx.showToast({ icon: 'none', title: `${fileType === 'image' ? '图片' : '视频'}大小超过限制` });

if (isOverSize(size, sizeLimit)) {
let title = `${fileType === 'image' ? '图片' : '视频'}大小超过限制`;

if (typeof sizeLimit !== 'number') {
title = sizeLimit.message.replace('{sizeLimit}', sizeLimit?.size);
}
wx.showToast({ icon: 'none', title });
return;
}

const name = this.getRandFileName(tempFilePath);
files.push({
name,
Expand Down Expand Up @@ -237,10 +245,17 @@ export default class Upload extends SuperComponent {
// 支持单/多文件
res.tempFiles.forEach((temp) => {
const { size, type: fileType, path: tempFilePath, ...res } = temp;
if (sizeLimit && size > sizeLimit) {
wx.showToast({ icon: 'none', title: `${fileType === 'image' ? '图片' : '视频'}大小超过限制` });

if (isOverSize(size, sizeLimit)) {
let title = `${fileType === 'image' ? '图片' : '视频'}大小超过限制`;

if (typeof sizeLimit !== 'number') {
title = sizeLimit.message.replace('{sizeLimit}', sizeLimit?.size);
}
wx.showToast({ icon: 'none', title });
return;
}

const name = this.getRandFileName(tempFilePath);
files.push({
name,
Expand Down