Skip to content

Commit

Permalink
fix type fileManger
Browse files Browse the repository at this point in the history
  • Loading branch information
zjy365 committed Apr 29, 2024
1 parent a2fb808 commit cd2315c
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 86 deletions.
5 changes: 3 additions & 2 deletions frontend/providers/applaunchpad/data/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ launchpad:
default:
cpu: [100, 200, 500, 1000, 2000, 3000, 4000, 8000]
memory: [64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384]
uploadLimit: 200 # MB
downloadLimit: 500 # MB
fileManger:
uploadLimit: 50 # MB
downloadLimit: 100 # MB
1 change: 0 additions & 1 deletion frontend/providers/applaunchpad/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import NProgress from 'nprogress'; //nprogress module
import { useEffect, useState } from 'react';
import { EVENT_NAME } from 'sealos-desktop-sdk';
import { createSealosApp, sealosApp } from 'sealos-desktop-sdk/app';
import { getPlatformEnv } from '@/api/platform';
import '@/styles/reset.scss';
import 'nprogress/nprogress.css';
import '@sealos/driver/src/driver.css';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await form.parse(req);
const data = await task;

jsonRes(res, { data: data });
jsonRes(res, { data });
} catch (err: any) {
console.log(err, 'err');
jsonRes(res, {
code: 500,
error: err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { Coin } from '@/constants/app';
import { jsonRes } from '@/services/backend/response';
import type { AppConfigType, FormSliderListType } from '@/types';
import type { AppConfigType, FileMangerType, FormSliderListType } from '@/types';
import { readFileSync } from 'fs';
import { Coin } from '@/constants/app';
import * as yaml from 'js-yaml';
import type { NextApiRequest, NextApiResponse } from 'next';

// todo make response type to be more specific and clear.
export type Response = {
Expand All @@ -13,6 +13,7 @@ export type Response = {
SHOW_EVENT_ANALYZE: boolean;
FORM_SLIDER_LIST_CONFIG: FormSliderListType;
CURRENCY: Coin;
fileMangerConfig: FileMangerType;
};

export const defaultAppConfig: AppConfigType = {
Expand All @@ -39,9 +40,11 @@ export const defaultAppConfig: AppConfigType = {
default: {
cpu: [100, 200, 500, 1000, 2000, 3000, 4000, 8000],
memory: [64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384]
},
uploadLimit: 200,
downloadLimit: 500
}
},
fileManger: {
uploadLimit: 50,
downloadLimit: 100
}
}
};
Expand All @@ -66,6 +69,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
INGRESS_SECRET: global.AppConfig.launchpad.ingressTlsSecretName,
SHOW_EVENT_ANALYZE: global.AppConfig.launchpad.eventAnalyze.enabled,
FORM_SLIDER_LIST_CONFIG: global.AppConfig.launchpad.appResourceFormSliderConfig,
fileMangerConfig: global.AppConfig.launchpad.fileManger,
CURRENCY: Coin.shellCoin
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import MyIcon from '@/components/Icon';
import { useSelectFile } from '@/hooks/useSelectFile';
import { MOCK_APP_DETAIL, MOCK_PODS } from '@/mock/apps';
import { useAppStore } from '@/store/app';
import { UPLOAD_LIMIT } from '@/store/static';
import type { PodDetailType } from '@/types/app';
import { TFile } from '@/utils/kubeFileSystem';
import { formatSize, formatTime } from '@/utils/tools';
Expand Down Expand Up @@ -46,9 +47,8 @@ import {
import { MyTooltip, SealosMenu, useMessage } from '@sealos/ui';
import { useQuery } from '@tanstack/react-query';
import { useTranslation } from 'next-i18next';
import { MouseEvent, useEffect, useMemo, useState } from 'react';
import { MouseEvent, useMemo, useState } from 'react';
import styles from '../index.module.scss';
import { useGlobalStore } from '@/store/global';

type HandleType = 'delete' | 'rename' | 'mkdir-file' | 'mkdir' | 'download';

Expand All @@ -67,7 +67,6 @@ const PodFile = ({
podAlias: string;
setPodDetail: (name: string) => void;
}) => {
const { formSliderListConfig } = useGlobalStore();
const { t } = useTranslation();
const theme = useTheme();
const { message } = useMessage();
Expand Down Expand Up @@ -255,10 +254,10 @@ const PodFile = ({
};

const uploadFile = async (files: File[]) => {
setIsUploadLoading(true);
try {
setIsUploadLoading(true);
const filteredFiles = files.filter((file) => {
if (file.size > formSliderListConfig.uploadLimit) {
if (file.size > UPLOAD_LIMIT * 1024 * 1024) {
message({
status: 'info',
title: t('File is too large tip', { name: file.name })
Expand All @@ -281,9 +280,11 @@ const PodFile = ({
);
});
await Promise.all(uploadPromises);
setIsUploadLoading(false);
refetch();
} catch (error) {}
} catch (error) {
refetch();
}
setIsUploadLoading(false);
};

const createFolder = async () => {
Expand Down
4 changes: 1 addition & 3 deletions frontend/providers/applaunchpad/src/store/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ export const useGlobalStore = create<State>()(
[defaultSliderKey]: {
cpu: [100, 200, 500, 1000, 2000, 3000, 4000, 8000],
memory: [64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384]
},
uploadLimit: 200,
downloadLimit: 500
}
},
initFormSliderList(res) {
if (!res) return;
Expand Down
4 changes: 4 additions & 0 deletions frontend/providers/applaunchpad/src/store/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export let DOMAIN_PORT = '';
export let INGRESS_SECRET = 'wildcard-cert';
export let SHOW_EVENT_ANALYZE = false;
export let CURRENCY = Coin.shellCoin;
export let UPLOAD_LIMIT = 50;
export let DOWNLOAD_LIMIT = 100;

export const loadInitData = async () => {
try {
Expand All @@ -15,6 +17,8 @@ export const loadInitData = async () => {
INGRESS_SECRET = res.INGRESS_SECRET;
SHOW_EVENT_ANALYZE = res.SHOW_EVENT_ANALYZE;
CURRENCY = res.CURRENCY;
UPLOAD_LIMIT = res.fileMangerConfig.uploadLimit;
DOWNLOAD_LIMIT = res.fileMangerConfig.downloadLimit;

return {
SEALOS_DOMAIN,
Expand Down
11 changes: 8 additions & 3 deletions frontend/providers/applaunchpad/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ export interface YamlItemType {
value: string;
}

export type FormSliderListType = {
[defaultSliderKey]: {
export type FormSliderListType = Record<
string,
{
cpu: number[];
memory: number[];
};
}
>;

export type FileMangerType = {
uploadLimit: number;
downloadLimit: number;
};
Expand All @@ -40,6 +44,7 @@ export type AppConfigType = {
};
};
appResourceFormSliderConfig: FormSliderListType;
fileManger: FileMangerType;
// todo: add gpu appResourceFormSliderConfig config.yaml and codes here
// gpu?: {
// cpu: number[];
Expand Down
96 changes: 34 additions & 62 deletions frontend/providers/applaunchpad/src/utils/kubeFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,74 +310,46 @@ export class KubeFileSystem {
path: string;
file: Readable;
}) {
return new Promise<string>((res, rej) => {
let localFileSize = 0;
const base64Encoder = new Base64Encode({ lineLength: 76 });
file.on('data', (chunk) => {
localFileSize += chunk.length;
});
this.execCommand(
let localFileSize = 0;
const base64Encoder = new Base64Encode({ lineLength: 76 });
file.on('data', (chunk) => {
localFileSize += chunk.length;
});

try {
await this.execCommand(
namespace,
podName,
containerName,
['sh', '-c', `dd of=${path}.tmp status=none bs=32767 `],
file.pipe(base64Encoder)
)
.then((e) => {
let attempts = 0;
const maxAttempts = 10;
const intervalTime = 3000;
return new Promise<string>((resolve, reject) => {
const interval = setInterval(() => {
this.execCommand(namespace, podName, containerName, [
'sh',
'-c',
`stat -c %s ${path}.tmp`
])
.then((v) => {
const sizeIncreaseThreshold = localFileSize * 1.3;
if (parseInt(v.trim()) > sizeIncreaseThreshold) {
clearInterval(interval);
clearTimeout(timeout);
resolve('success');
} else {
attempts++;
if (attempts >= maxAttempts) {
clearInterval(interval);
clearTimeout(timeout);
reject(
'File integrity check failed after maximum attempts. Please retry the upload.'
);
}
}
})
.catch(() => {
clearInterval(interval);
clearTimeout(timeout);
reject('Error during file size check. Please retry the upload.');
});
}, intervalTime);
);
let attempts = 0;
const maxAttempts = 10;
const intervalTime = 3000;

const timeout = setTimeout(() => {
clearInterval(interval);
reject('File integrity check timed out. Please retry the upload.');
}, intervalTime * maxAttempts);
});
})
.then((data) => {
if (data === 'success') {
return this.execCommand(namespace, podName, containerName, [
'sh',
'-c',
`base64 -d ${path}.tmp > ${path} && rm -rf ${path}.tmp`
]);
} else {
rej('upload err');
}
})
.then((data) => res('success'))
.catch((e) => rej('upload err'));
});
while (attempts < maxAttempts) {
await new Promise((resolve) => setTimeout(resolve, intervalTime));
const v = await this.execCommand(namespace, podName, containerName, [
'sh',
'-c',
`stat -c %s ${path}.tmp`
]);
const sizeIncreaseThreshold = localFileSize * 1.3;
if (parseInt(v.trim()) > sizeIncreaseThreshold) {
await this.execCommand(namespace, podName, containerName, [
'sh',
'-c',
`base64 -d ${path}.tmp > ${path} && rm -rf ${path}.tmp`
]);
return 'success';
}
attempts++;
}
throw new Error('File integrity check failed after maximum attempts.');
} catch (error) {
throw new Error(`Upload error`);
}
}

async md5sum({
Expand Down

0 comments on commit cd2315c

Please sign in to comment.