Skip to content

Instantly share code, notes, and snippets.

@PierreMacherel
Last active August 5, 2022 06:18
Show Gist options
  • Save PierreMacherel/742ffc1babefdb4667c2f29ec4a9df9f to your computer and use it in GitHub Desktop.
Save PierreMacherel/742ffc1babefdb4667c2f29ec4a9df9f to your computer and use it in GitHub Desktop.
Upload picture to nodocb using the rest api

to use:

npm install

then

npm run start -- baseURL token project_id table_id pathToImage

where

  • baseURL is the name of your nocodb server. ex: https://myNocodbServer.com
  • token obtained from copy auth Token upper right button
  • project_id your project id (find it in swagger)
  • table_id your table id (find it in swagger)
  • pathToImage path of the image to upload

You need to modify the code for the row upload (line 57-58 of index.ts: PicColumn: pic).

Note: Use it at your own risk.

import { createReadStream } from "fs";
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
import Axios from "axios-observable";
import FormData from "form-data";
const startingArgumentIndex = 2;
if (process.argv[startingArgumentIndex] === "--help") {
console.log(
`usage: npm run start -- baseURL token project_id table_id pathToImage`
);
process.exit(1);
}
const [baseURL, token, project_id, table_id, imagePath]: string[] = process.argv.slice(startingArgumentIndex);
interface photoMetaResponse {
url: string;
title: string;
mimetype: string;
size: number;
}
/**
* @param imagePath ex: "C:/Users/BABAR/Pictures/MyPicture.jpeg"
* @param authToken token optained from `copy auth Token`.
* @param baseURL name of your nocodb server
* @param project_id extracti if from swagger api example
* @returns
*/
function uploadImage(imagePath: string, authToken: string, baseURL: string, project_id: string): Observable<photoMetaResponse> {
const jsonParam = JSON.stringify({ "api" : "xcAttachmentUpload", project_id, "dbAlias" : "db", "args" : {}});
const formData = new FormData();
formData.append("file", createReadStream(imagePath));
formData.append("json", jsonParam);
return Axios.request<photoMetaResponse>({
url: `${baseURL}/dashboard`,
data: formData,
headers: {
"Content-Type": `multipart/form-data; boundary=${formData.getBoundary()}`, // the important part
"xc-auth": authToken
},
method: "post",
params: { project_id }
}).pipe(
map(x => x.data)
);
}
uploadImage(imagePath, token, baseURL, project_id)
.pipe(
map(v => JSON.stringify([v])),
)
.subscribe({next: pic => {
Axios.request<unknown>({
url: `${baseURL}/nc/${project_id}/api/v1/${table_id}`,
data: {
"title": "my Picture !",
picColumn: pic // change here to uppload a new row with correct header. picColumn must be of type "attachement"
},
headers: {
"Content-Type": "application/json",
"xc-auth": token
},
method: "post",
params: { project_id }
}).subscribe();
},
error: console.error
});
{
"name": "nocodb-picture-upload",
"version": "0.0.2",
"description": "upload picture in nocodb",
"main": "index.ts",
"scripts": {
"build": "tsc --project tsconfig.json",
"start": "ts-node --project ./tsconfig.json ./index.ts"
},
"keywords": [
"nocodb"
],
"author": "PierreMacherel",
"license": "MIT",
"dependencies": {
"axios": "^0.25.0",
"axios-observable": "^1.3.0",
"form-data": "^4.0.0",
"rxjs": "^7.5.3"
},
"devDependencies": {
"@types/node": "^17.0.16",
"ts-node": "^10.5.0"
}
}
{
"compilerOptions": {
"noImplicitReturns": true,
"noImplicitAny": true,
"declarationDir": "./dist",
"outDir": "./dist",
"preserveConstEnums": false,
"removeComments": true,
"target": "ES2019",
"module": "commonJs",
"stripInternal": true,
"strict": true,
"experimentalDecorators": true,
"noEmitHelpers": false,
"declaration": true,
"sourceMap": true,
"moduleResolution": "node",
"esModuleInterop": true,
},
"files": [
"./index.ts"
]
}
@kctdfh
Copy link

kctdfh commented Feb 6, 2022

Hey - I get a ts compile error but don't know any ts and can't debug. This is my log:

[{
	"resource": ".../index.ts",
	"owner": "typescript",
	"code": "2554",
	"severity": 8,
	"message": "Expected 0 arguments, but got 1.",
	"source": "ts",
	"startLineNumber": 10,
	"startColumn": 9,
	"endLineNumber": 10,
	"endColumn": 80
},{
	"resource": ".../index.ts",
	"owner": "typescript",
	"code": "2304",
	"severity": 8,
	"message": "Cannot find name 'exit'.",
	"source": "ts",
	"startLineNumber": 12,
	"startColumn": 5,
	"endLineNumber": 12,
	"endColumn": 9
},{
	"resource": ".../index.ts",
	"owner": "typescript",
	"code": "2739",
	"severity": 8,
	"message": "Type 'Observable<unknown>' is missing the following properties from type 'Observable<photoMetaResponse>': _isScalar, _trySubscribe, _subscribe",
	"source": "ts",
	"startLineNumber": 35,
	"startColumn": 5,
	"endLineNumber": 46,
	"endColumn": 7
},{
	"resource": ".../index.ts",
	"owner": "typescript",
	"code": "2345",
	"severity": 8,
	"message": "Argument of type 'OperatorFunction<AxiosResponse<photoMetaResponse>, photoMetaResponse>' is not assignable to parameter of type 'OperatorFunction<AxiosResponse<photoMetaResponse>, unknown>'.\n  Types of parameters 'source' and 'source' are incompatible.\n    Type 'Observable<AxiosResponse<photoMetaResponse>>' is missing the following properties from type 'Observable<AxiosResponse<photoMetaResponse>>': _isScalar, _trySubscribe, _subscribe",
	"source": "ts",
	"startLineNumber": 45,
	"startColumn": 9,
	"endLineNumber": 45,
	"endColumn": 22
}]

Cheers

@PierreMacherel
Copy link
Author

I rectified the gist to fix typescript error, remove useless packages and update packages. I tried the new version, and it is now working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment