Skip to content

Commit

Permalink
fix: type import url content-type may be empty (#421)
Browse files Browse the repository at this point in the history
* fix: import url content-type may be empty

* chore: update toolbar search placehoder text

* fix: type error
  • Loading branch information
caoxing9 committed Mar 11, 2024
1 parent bb92bd9 commit 665e42b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class ImportOpenApiService {
const { attachmentUrl, fileType } = analyzeRo;
const importer = importerFactory(fileType, {
url: attachmentUrl,
fileType,
});

return await importer.genColumns();
Expand All @@ -35,6 +36,7 @@ export class ImportOpenApiService {

const importer = importerFactory(fileType, {
url: attachmentUrl,
fileType,
});
const fieldsRo = columnInfo.map((col, index) => {
return {
Expand Down
18 changes: 12 additions & 6 deletions apps/nestjs-backend/src/features/import/open-api/import.class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BadRequestException } from '@nestjs/common';
import type { IValidateTypes } from '@teable/core';
import { getUniqName, FieldType, SUPPORTEDTYPE } from '@teable/core';
import { getUniqName, FieldType, SUPPORTEDTYPE, importTypeMap } from '@teable/core';
import { axios } from '@teable/openapi';
import { zip } from 'lodash';
import Papa from 'papaparse';
Expand Down Expand Up @@ -104,24 +104,27 @@ export class CsvImporter extends Importer {
FieldType.LongText,
FieldType.SingleLineText,
];
constructor(public config: { url: string }) {
constructor(public config: { url: string; fileType: SUPPORTEDTYPE }) {
super(config);
}
getSupportedFieldTypes() {
return CsvImporter.SUPPORTEDTYPE;
}
async getFile() {
const { url } = this.config;
const { url, fileType } = this.config;
const { data: stream } = await axios.get(url, {
responseType: 'stream',
});
const fileFormat = stream?.headers?.['content-type']?.split(';')?.[0];

if (!CsvImporter.SUPPORTFILETYPE.includes(fileFormat)) {
const supportType = importTypeMap[fileType].acceptHeaders;

if (fileFormat && !supportType.includes(fileFormat)) {
throw new BadRequestException(
`File format is not supported, only ${CsvImporter.SUPPORTFILETYPE.join(', ')} are supported,`
`File format is not supported, only ${supportType.join(',')} are supported,`
);
}

return stream;
}
async parse(): Promise<unknown[]> {
Expand Down Expand Up @@ -179,7 +182,10 @@ export class CsvImporter extends Importer {
}
}

export const importerFactory = (type: SUPPORTEDTYPE, config: { url: string }) => {
export const importerFactory = (
type: SUPPORTEDTYPE,
config: { url: string; fileType: SUPPORTEDTYPE }
) => {
switch (type) {
case SUPPORTEDTYPE.CSV:
return new CsvImporter(config);
Expand Down
2 changes: 1 addition & 1 deletion packages/common-i18n/src/locales/en/sdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"cancel": "Cancel",
"confirm": "Confirm",
"search": {
"placeholder": "Search records",
"placeholder": "Search",
"empty": "No results found."
},
"selectPlaceHolder": "Select..."
Expand Down
2 changes: 1 addition & 1 deletion packages/common-i18n/src/locales/zh/sdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"cancel": "取消",
"confirm": "确认",
"search": {
"placeholder": "搜索记录",
"placeholder": "搜索",
"empty": "未找到结果。"
},
"selectPlaceHolder": "请选择..."
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/import/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { SUPPORTEDTYPE } from './types';

export const importTypeMap = {
[SUPPORTEDTYPE.CSV]: {
accept: 'text/csv',
exampleUrl: 'https://www.example.com/file.csv',
acceptHeaders: ['text/csv', 'text/plain'],
},
[SUPPORTEDTYPE.EXCEL]: {
accept:
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel',
exampleUrl: 'https://www.example.com/file.xlsx',
acceptHeaders: [
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-excel',
],
},
};
1 change: 1 addition & 0 deletions packages/core/src/import/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './types';
export * from './constant';

0 comments on commit 665e42b

Please sign in to comment.