diff --git a/src/api/auth.cjs b/src/api/auth.cjs index 43e8603..fd92d15 100644 --- a/src/api/auth.cjs +++ b/src/api/auth.cjs @@ -1,4 +1,3 @@ -const express = require('express'); const axios = require('axios'); let auth_status = 0; @@ -23,17 +22,18 @@ function verify_auth_status() { } async function get_access_token(host, session_id) { - axios.post(`${host}/api/miauth/${session_id}/check`) - .then((data) => { - const response = data.json(); - - if(response.ok == true) { - console.log(response.token); - return response.token; - } else { - return false; + const response = await axios.post(`${host}/api/miauth/${session_id}/check`, null, { + headers: { + 'Content-Type': 'application/json' } - }); + }) + + if(response.data.ok == true) { + console.log("Successfully retrieved access token"); + return response.data.token; + } else { + return false; + } }; module.exports.check_auth_status = check_auth_status; diff --git a/src/api/post.cjs b/src/api/post.cjs index d81ba2f..f270653 100644 --- a/src/api/post.cjs +++ b/src/api/post.cjs @@ -1,16 +1,31 @@ const axios = require('axios'); +const fs = require('fs'); async function create_image(host, access_token, file_path) { - axios.post(`${host}/api/drive/files/create`, { + const create = 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 create; }; +async function create_note(host, access_token, text, media_ids, visibility) { + axios.post(`${host}/api/notes/create`, { + text: text, + mediaIds: media_ids, + visibility: visibility + }, { + headers: { + 'Authorization': `Bearer ${access_token}` + } + }).then((data) => { + return data.json(); + }); +} + module.exports.create_image = create_image; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 60b2527..555caf0 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,15 @@ import { v4 as uuidv4 } from 'uuid'; -import fs, { createReadStream } from 'node:fs'; +import fs from 'node:fs'; import path from 'node:path'; import express from 'express'; -import axios from 'axios'; -import { fileFromPath } from 'formdata-node/file-from-path'; import * as Misskey from 'misskey-js'; import * as Post from './api/post.cjs'; import * as Auth from './api/auth.cjs'; 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"))); +const config = JSON.parse(fs.readFileSync(path.join(__dirname, '/config/config.json'))); +const token_file = JSON.parse(fs.readFileSync(path.join(__dirname, '/config/token.json'))); let app = express(); let port = process.env.PORT || 4000; @@ -60,7 +58,10 @@ app.get('/callback', function(req, res) { }); let static_image_path = path.join(__dirname, '/data/current_radar.jpg'); - Post.create_image(host, access_token, static_image_path); + const upload_image = await Post.create_image(host, access_token, static_image_path); + + console.log(upload_image); + })(); app.listen(port);