implemented image uploading

This commit is contained in:
Win 2024-07-09 20:49:07 +07:00
parent 1a2c418629
commit 5aca0fbcd0
5 changed files with 121 additions and 11 deletions

View File

@ -3,6 +3,6 @@
"app": { "app": {
"name": "bkkraindarbot", "name": "bkkraindarbot",
"callback": "http://localhost:4000/callback", "callback": "http://localhost:4000/callback",
"permission": "write:notes,write:following,read:drive" "permission": "write:notes,write:following,write:drive"
} }
} }

BIN
data/current_radar.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 317 KiB

89
package-lock.json generated
View File

@ -9,7 +9,9 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^1.7.2",
"express": "^4.19.2", "express": "^4.19.2",
"formdata-node": "^6.0.3",
"misskey-js": "^2024.5.0", "misskey-js": "^2024.5.0",
"node-fetch": "^3.3.2", "node-fetch": "^3.3.2",
"uuid": "^10.0.0" "uuid": "^10.0.0"
@ -34,6 +36,23 @@
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==",
"license": "MIT"
},
"node_modules/axios": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz",
"integrity": "sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==",
"license": "MIT",
"dependencies": {
"follow-redirects": "^1.15.6",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/body-parser": { "node_modules/body-parser": {
"version": "1.20.2", "version": "1.20.2",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz",
@ -86,6 +105,18 @@
"url": "https://github.com/sponsors/ljharb" "url": "https://github.com/sponsors/ljharb"
} }
}, },
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"license": "MIT",
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/content-disposition": { "node_modules/content-disposition": {
"version": "0.5.4", "version": "0.5.4",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
@ -157,6 +188,15 @@
"url": "https://github.com/sponsors/ljharb" "url": "https://github.com/sponsors/ljharb"
} }
}, },
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"license": "MIT",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/depd": { "node_modules/depd": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
@ -316,6 +356,49 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/follow-redirects": {
"version": "1.15.6",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"license": "MIT",
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"license": "MIT",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/formdata-node": {
"version": "6.0.3",
"resolved": "https://registry.npmjs.org/formdata-node/-/formdata-node-6.0.3.tgz",
"integrity": "sha512-8e1++BCiTzUno9v5IZ2J6bv4RU+3UKDmqWUQD0MIMVCd9AdhWkO1gw57oo1mNEX1dMq2EGI+FbWz4B92pscSQg==",
"license": "MIT",
"engines": {
"node": ">= 18"
}
},
"node_modules/formdata-polyfill": { "node_modules/formdata-polyfill": {
"version": "4.0.10", "version": "4.0.10",
"resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
@ -648,6 +731,12 @@
"node": ">= 0.10" "node": ">= 0.10"
} }
}, },
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==",
"license": "MIT"
},
"node_modules/qs": { "node_modules/qs": {
"version": "6.11.0", "version": "6.11.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",

View File

@ -12,7 +12,9 @@
"license": "ISC", "license": "ISC",
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"axios": "^1.7.2",
"express": "^4.19.2", "express": "^4.19.2",
"formdata-node": "^6.0.3",
"misskey-js": "^2024.5.0", "misskey-js": "^2024.5.0",
"node-fetch": "^3.3.2", "node-fetch": "^3.3.2",
"uuid": "^10.0.0" "uuid": "^10.0.0"

View File

@ -1,14 +1,19 @@
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import fs from 'fs'; import fs, { createReadStream } from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import express from 'express'; import express from 'express';
import axios from 'axios';
import { fileFromPath } from 'formdata-node/file-from-path';
import * as Misskey from 'misskey-js'; import * as Misskey from 'misskey-js';
const __dirname = path.resolve();
const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json")));
const token_file = JSON.parse(fs.readFileSync(path.resolve("config", "token.json")));
let app = express(); let app = express();
let port = process.env.PORT || 4000; let port = process.env.PORT || 4000;
const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json")));
let host = config.host; let host = config.host;
let app_name = config.app.name; let app_name = config.app.name;
@ -60,14 +65,30 @@ async function get_access_token(host, session_id) {
if(data.ok == true) { if(data.ok == true) {
return data.token; return data.token;
} else { } else {
return false; // error occured return false; // error occurred
} }
}; };
async function create_image(access_token, file_path) {
axios.post(`${host}/api/drive/files/create`, {
file: fs.createReadStream(file_path)
}, {
headers: {
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${access_token}`
}
}).then((data) => {
return data;
});
// return info:
// https://misskey.winscloud.net/api-doc#tag/drive/operation/drive___files___create
};
(async () => { (async () => {
let access_token = 0; let access_token = false;
const token_file = JSON.parse(fs.readFileSync(path.resolve("config", "token.json")));
if(!token_file.token || token_file.token == "" || token_file.token == false) { // if existing token not exist if(!token_file.token) { // if existing token not exist
await check_auth_status() await check_auth_status()
access_token = await get_access_token(host, session_id); access_token = await get_access_token(host, session_id);
@ -88,10 +109,8 @@ async function get_access_token(host, session_id) {
credential: access_token credential: access_token
}); });
const meta = await cli.request('notes/create', { let static_image_path = path.join(__dirname, '/data/current_radar.jpg');
text: "hi from misskey api!" create_image(access_token, static_image_path);
});
console.log(meta);
})(); })();
app.listen(port); app.listen(port);