Browse Source

refactor: redesign update attachment api

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/1668/head
Pranav C 3 years ago
parent
commit
e9f730de15
  1. 12
      packages/nc-gui/components/project/spreadsheet/components/editableCell/editableAttachmentCell.vue
  2. 47
      packages/nocodb-sdk/src/lib/Api.ts
  3. 35
      packages/nocodb/src/lib/noco/meta/api/attachmentApis.ts
  4. 6
      packages/nocodb/src/lib/noco/meta/api/publicApis/publicDataApis.ts
  5. 2
      packages/nocodb/src/lib/utils/projectAcl.ts
  6. 99
      scripts/sdk/swagger.json

12
packages/nc-gui/components/project/spreadsheet/components/editableCell/editableAttachmentCell.vue

@ -333,14 +333,14 @@ export default {
this.uploading = true this.uploading = true
for (const file of this.$refs.file.files) { for (const file of this.$refs.file.files) {
try { try {
const data = await this.$api.dbViewRow.upload( const data = await this.$api.storage.upload(
'noco', {
this.projectName, path: ['noco', this.projectName, this.meta.title, this.column.title].join('/')
this.meta.title, }, {
this.column.title, {
files: file, files: file,
json: '{}' json: '{}'
}) }
)
this.localState.push(...data) this.localState.push(...data)
} catch (e) { } catch (e) {

47
packages/nocodb-sdk/src/lib/Api.ts

@ -2778,30 +2778,6 @@ export class Api<
wrapped: true, wrapped: true,
...params, ...params,
}), }),
/**
* No description
*
* @tags DB View row
* @name Upload
* @summary Attachment
* @request POST:/api/v1/db/data-attachment/{orgs}/{projectName}/{tableName}/{columnName}
*/
upload: (
orgs: string,
projectName: string,
tableName: string,
columnName: string,
data: { files?: any; json?: string },
params: RequestParams = {}
) =>
this.request<any, any>({
path: `/api/v1/db/data-attachment/${orgs}/${projectName}/${tableName}/${columnName}`,
method: 'POST',
body: data,
type: ContentType.FormData,
...params,
}),
}; };
public = { public = {
/** /**
@ -3355,4 +3331,27 @@ export class Api<
...params, ...params,
}), }),
}; };
storage = {
/**
* No description
*
* @tags Storage
* @name Upload
* @summary Attachment
* @request POST:/api/v1/db/storage/upload
*/
upload: (
query: { path: string },
data: { files?: any; json?: string },
params: RequestParams = {}
) =>
this.request<any, any>({
path: `/api/v1/db/storage/upload`,
method: 'POST',
query: query,
body: data,
type: ContentType.FormData,
...params,
}),
};
} }

35
packages/nocodb/src/lib/noco/meta/api/attachmentApis.ts

@ -12,12 +12,9 @@ import NcPluginMgrv2 from '../helpers/NcPluginMgrv2';
// const storageAdapter = new Local(); // const storageAdapter = new Local();
export async function upload(req: Request, res: Response) { export async function upload(req: Request, res: Response) {
const filePath = sanitizeUrlPath([ const filePath = sanitizeUrlPath(
req.params.orgs, req.query?.path?.toString()?.split('/') || ['']
req.params.projectName, );
req.params.tableName,
req.params.columnName
]);
const destPath = path.join('nc', 'uploads', ...filePath); const destPath = path.join('nc', 'uploads', ...filePath);
const storageAdapter = await NcPluginMgrv2.storageAdapter(); const storageAdapter = await NcPluginMgrv2.storageAdapter();
@ -31,9 +28,9 @@ export async function upload(req: Request, res: Response) {
); );
if (!url) { if (!url) {
url = `${ url = `${(req as any).ncSiteUrl}/download/${filePath.join(
(req as any).ncSiteUrl '/'
}/api/v1/db/data-attachment/${filePath.join('/')}/${fileName}`; )}/${fileName}`;
} }
return { return {
@ -53,11 +50,11 @@ export async function upload(req: Request, res: Response) {
export async function fileRead(req, res) { export async function fileRead(req, res) {
try { try {
const storageAdapter = await NcPluginMgrv2.storageAdapter(); const storageAdapter = await NcPluginMgrv2.storageAdapter();
// const type = mimetypes[path.extname(req.params.fileName).slice(1)] || 'text/plain'; // const type = mimetypes[path.extname(req.s.fileName).slice(1)] || 'text/plain';
const type = const type =
mimetypes[ mimetypes[
path path
.extname(req.params.fileName) .extname(req.params?.[0])
.split('/') .split('/')
.pop() .pop()
.slice(1) .slice(1)
@ -68,11 +65,10 @@ export async function fileRead(req, res) {
path.join( path.join(
'nc', 'nc',
'uploads', 'uploads',
req.params.orgs, req.params?.[0]
req.params.projectName, ?.split('/')
req.params.tableName, .filter(p => p !== '..')
req.params.columnName, .join('/')
req.params.fileName
) )
) )
); );
@ -122,15 +118,12 @@ export function sanitizeUrlPath(paths) {
} }
router.post( router.post(
'/api/v1/db/data-attachment/:orgs/:projectName/:tableName/:columnName', '/api/v1/db/storage/upload',
multer({ multer({
storage: multer.diskStorage({}) storage: multer.diskStorage({})
}).any(), }).any(),
ncMetaAclMw(upload, 'upload') ncMetaAclMw(upload, 'upload')
); );
router.get( router.get(/^\/download\/(.+)$/, catchError(fileRead));
'/api/v1/db/data-attachment/:orgs/:projectName/:tableName/:columnName/:fileName',
catchError(fileRead)
);
export default router; export default router;

6
packages/nocodb/src/lib/noco/meta/api/publicApis/publicDataApis.ts

@ -141,9 +141,9 @@ async function dataInsert(
); );
if (!url) { if (!url) {
url = `${ url = `${(req as any).ncSiteUrl}/download/${filePath.join(
(req as any).ncSiteUrl '/'
}/api/v1/db/data-attachment/${filePath.join('/')}/${fileName}`; )}/${fileName}`;
} }
attachments[fieldName].push({ attachments[fieldName].push({

2
packages/nocodb/src/lib/utils/projectAcl.ts

@ -208,7 +208,9 @@ export default {
passwordChange: true, passwordChange: true,
projectList: true projectList: true
}, },
super: '*',
user: { user: {
upload: true,
passwordChange: true, passwordChange: true,
pluginList: true, pluginList: true,
pluginRead: true, pluginRead: true,

99
scripts/sdk/swagger.json

@ -3282,68 +3282,6 @@
} }
}, },
"/api/v1/db/data-attachment/{orgs}/{projectName}/{tableName}/{columnName}": {
"post": {
"summary": "Attachment",
"operationId": "db-view-row-upload",
"responses": {},
"tags": [
"DB View row"
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"files": {},
"json": {
"type": "string"
}
}
}
}
},
"description": ""
}
},
"parameters": [
{
"schema": {
"type": "string"
},
"name": "orgs",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "projectName",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "tableName",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "columnName",
"in": "path",
"required": true
}
]
},
"/api/v1/db/public/shared-view/{sharedViewUuid}/rows": { "/api/v1/db/public/shared-view/{sharedViewUuid}/rows": {
"parameters": [ "parameters": [
{ {
@ -4749,6 +4687,43 @@
"DB Table Row" "DB Table Row"
] ]
} }
},
"/api/v1/db/storage/upload": {
"post": {
"summary": "Attachment",
"operationId": "storage-upload",
"responses": {},
"tags": [
"Storage"
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"files": {},
"json": {
"type": "string"
}
}
}
}
},
"description": ""
},
"parameters": [
{
"schema": {
"type": "string"
},
"name": "path",
"in": "query",
"required": true
}
]
}
} }
}, },
"components": { "components": {

Loading…
Cancel
Save