Browse Source

Merge pull request #6824 from nocodb/fix/attachment-original-name

fix(nocodb): attachment original name
pull/6829/head
Raju Udava 1 year ago committed by GitHub
parent
commit
80a20a111a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      packages/nocodb/src/helpers/stringHelpers.ts
  2. 9
      packages/nocodb/src/services/attachments.service.ts
  3. 9
      packages/nocodb/src/services/public-datas.service.ts

4
packages/nocodb/src/helpers/stringHelpers.ts

@ -3,3 +3,7 @@ import crypto from 'crypto';
export function randomTokenString(): string { export function randomTokenString(): string {
return crypto.randomBytes(40).toString('hex'); return crypto.randomBytes(40).toString('hex');
} }
export function utf8ify(str: string): string {
return Buffer.from(str, 'latin1').toString('utf8');
}

9
packages/nocodb/src/services/attachments.service.ts

@ -8,6 +8,7 @@ import NcPluginMgrv2 from '~/helpers/NcPluginMgrv2';
import Local from '~/plugins/storage/Local'; import Local from '~/plugins/storage/Local';
import mimetypes, { mimeIcons } from '~/utils/mimeTypes'; import mimetypes, { mimeIcons } from '~/utils/mimeTypes';
import { PresignedUrl } from '~/models'; import { PresignedUrl } from '~/models';
import { utf8ify } from '~/helpers/stringHelpers';
@Injectable() @Injectable()
export class AttachmentsService { export class AttachmentsService {
@ -28,7 +29,8 @@ export class AttachmentsService {
const attachments = await Promise.all( const attachments = await Promise.all(
param.files?.map(async (file: any) => { param.files?.map(async (file: any) => {
const fileName = `${nanoid(18)}${path.extname(file.originalname)}`; const originalName = utf8ify(file.originalname);
const fileName = `${nanoid(18)}${path.extname(originalName)}`;
const url = await storageAdapter.fileCreate( const url = await storageAdapter.fileCreate(
slash(path.join(destPath, fileName)), slash(path.join(destPath, fileName)),
@ -46,11 +48,10 @@ export class AttachmentsService {
signedUrl?: string; signedUrl?: string;
} = { } = {
...(url ? { url } : {}), ...(url ? { url } : {}),
title: file.originalname, title: originalName,
mimetype: file.mimetype, mimetype: file.mimetype,
size: file.size, size: file.size,
icon: icon: mimeIcons[path.extname(originalName).slice(1)] || undefined,
mimeIcons[path.extname(file.originalname).slice(1)] || undefined,
}; };
const promises = []; const promises = [];

9
packages/nocodb/src/services/public-datas.service.ts

@ -14,6 +14,7 @@ import { getColumnByIdOrName } from '~/modules/datas/helpers';
import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2'; import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2';
import { mimeIcons } from '~/utils/mimeTypes'; import { mimeIcons } from '~/utils/mimeTypes';
import { Column, Model, Source, View } from '~/models'; import { Column, Model, Source, View } from '~/models';
import { utf8ify } from '~/helpers/stringHelpers';
// todo: move to utils // todo: move to utils
export function sanitizeUrlPath(paths) { export function sanitizeUrlPath(paths) {
@ -319,7 +320,8 @@ export class PublicDatasService {
fields[fieldName].uidt === UITypes.Attachment fields[fieldName].uidt === UITypes.Attachment
) { ) {
attachments[fieldName] = attachments[fieldName] || []; attachments[fieldName] = attachments[fieldName] || [];
const fileName = `${nanoid(18)}${path.extname(file.originalname)}`; const originalName = utf8ify(file.originalname);
const fileName = `${nanoid(18)}${path.extname(originalName)}`;
const url = await storageAdapter.fileCreate( const url = await storageAdapter.fileCreate(
slash(path.join('nc', 'uploads', ...filePath, fileName)), slash(path.join('nc', 'uploads', ...filePath, fileName)),
@ -337,11 +339,10 @@ export class PublicDatasService {
attachments[fieldName].push({ attachments[fieldName].push({
...(url ? { url } : {}), ...(url ? { url } : {}),
...(attachmentPath ? { path: attachmentPath } : {}), ...(attachmentPath ? { path: attachmentPath } : {}),
title: file.originalname, title: originalName,
mimetype: file.mimetype, mimetype: file.mimetype,
size: file.size, size: file.size,
icon: icon: mimeIcons[path.extname(originalName).slice(1)] || undefined,
mimeIcons[path.extname(file.originalname).slice(1)] || undefined,
}); });
} }
} }

Loading…
Cancel
Save