From c38654cab640d270743070a1e2b3f89975b51779 Mon Sep 17 00:00:00 2001 From: Joo Date: Sun, 1 May 2022 11:55:06 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=8A=A4=ED=83=80?= =?UTF-8?q?=EC=9D=BC=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jsconfig.json | 5 + src/main/apis/tistory-api.js | 114 ++++++++++-------- src/main/apis/tumblr-api.js | 84 +++++++------ .../tinymce/plugins/file-upload/plugin.js | 8 +- src/renderer/index.js | 1 - tsconfig.json | 7 -- 6 files changed, 111 insertions(+), 108 deletions(-) create mode 100644 jsconfig.json delete mode 100644 tsconfig.json diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..f34eb07 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "experimentalDecorators": true, + } +} diff --git a/src/main/apis/tistory-api.js b/src/main/apis/tistory-api.js index 4f596fa..ade4d8b 100644 --- a/src/main/apis/tistory-api.js +++ b/src/main/apis/tistory-api.js @@ -9,7 +9,10 @@ const appInfo = require('../appInfo') const ExternalOAuth2 = require('../oauth/ExternalOAuth2'); const OAuthRequestManager = require('../oauth/OAuthRequestManager'); -const errorHandler = (res) => { +const BASE_URL = 'https://www.tistory.com/apis' +const PROVIDER_ID = 'tistory' + +function _errorHandler(res) { if (!res.ok) { console.error("fetch failed", res) res.text().then(text => console.error("fetch body", text)) @@ -19,10 +22,44 @@ const errorHandler = (res) => { return res.json() } -const BASE_URL = 'https://www.tistory.com/apis' -const PROVIDER_ID = 'tistory' +function _uploadFile(accessToken, blogName, fileBlob, fileOption) { + let formdata = new FormData(); + formdata.append("access_token", accessToken) + formdata.append("output", "json") + formdata.append("blogName", blogName) + formdata.append("uploadedfile", fileBlob, fileOption) + + return fetch(BASE_URL + "/post/attach", { + method: 'post', + body: formdata + }) + .then(_errorHandler) + .then(res => res.tistory.url) +} -const requestAuth = (successHandler, failureHandler) => { + +function _tistoryPostToEditorPost(post) { + return { + id: post.id, + url: post.postUrl, + title: post.title, + date: post.date, + categoryId: post.categoryId, + state: post.visibility > 0? 'published' : 'draft', + tags: post.tags && post.tags.tag? [].concat(post.tags.tag) : [], + content: post.content? post.content : '' + } +} + +function _tistoryPostsToEditorPosts(tistoryPosts) { + let posts = tistoryPosts? [].concat(tistoryPosts) : [] + return posts.map(_tistoryPostToEditorPost) +} + + + + +function requestAuth(successHandler, failureHandler) { const oauthInfoReader = new OauthInfoReader() const oauth2 = new ExternalOAuth2(oauthInfoReader.getTistory()) OAuthRequestManager.saveRequestInfo("oauth", (searchParams) => { @@ -46,7 +83,7 @@ const requestAuth = (successHandler, failureHandler) => { oauth2.requestAuth({}) } -const fetchBlogInfo = (auth) => { +function fetchBlogInfo(auth) { return fetch(BASE_URL + "/blog/info?" + querystring.stringify({ access_token: auth.access_token, output: "json" @@ -55,10 +92,10 @@ const fetchBlogInfo = (auth) => { 'User-Agent': appInfo.userAgent } }) - .then(errorHandler) + .then(_errorHandler) } -const fetchUser = (auth) => { +function fetchUser(auth) { return fetch(BASE_URL + "/user?" + querystring.stringify({ access_token: auth.access_token, output: "json" @@ -67,26 +104,9 @@ const fetchUser = (auth) => { 'User-Agent': appInfo.userAgent } }) - .then(errorHandler) + .then(_errorHandler) } -function _tistoryPostToEditorPost(post) { - return { - id: post.id, - url: post.postUrl, - title: post.title, - date: post.date, - categoryId: post.categoryId, - state: post.visibility > 0? 'published' : 'draft', - tags: post.tags && post.tags.tag? [].concat(post.tags.tag) : [], - content: post.content? post.content : '' - } -} - -function _tistoryPostsToEditorPosts(tistoryPosts) { - let posts = tistoryPosts? [].concat(tistoryPosts) : [] - return posts.map(_tistoryPostToEditorPost) -} const fetchPosts = (auth, blogName, options) => { return fetch(BASE_URL + "/post/list?" + querystring.stringify({ @@ -100,7 +120,7 @@ const fetchPosts = (auth, blogName, options) => { 'User-Agent': appInfo.userAgent } }) - .then(errorHandler) + .then(_errorHandler) .then(res => ({ page: res.tistory.item.page, posts: _tistoryPostsToEditorPosts(res.tistory.item.posts), @@ -108,7 +128,7 @@ const fetchPosts = (auth, blogName, options) => { })) } -const fetchPost = (auth, blogName, postId) => { +function fetchPost(auth, blogName, postId) { return fetch(BASE_URL + "/post/read?" + querystring.stringify({ access_token: auth.access_token, output: "json", @@ -119,13 +139,13 @@ const fetchPost = (auth, blogName, postId) => { 'User-Agent': appInfo.userAgent } }) - .then(errorHandler) + .then(_errorHandler) .then(res => ({ post: _tistoryPostToEditorPost(res.tistory.item) })) } -const fetchCategories = (auth, blogName) => { +function fetchCategories(auth, blogName) { return fetch(BASE_URL + "/category/list?" + querystring.stringify({ access_token: auth.access_token, output: "json", @@ -135,11 +155,11 @@ const fetchCategories = (auth, blogName) => { 'User-Agent': appInfo.userAgent } }) - .then(errorHandler) + .then(_errorHandler) .then(res => res.tistory.item.categories) } -const savePost = (auth, blogName, post) => { +function savePost(auth, blogName, post) { let formdata = makePostFormData(auth, blogName, post) formdata.append("postId", post.id) @@ -151,11 +171,11 @@ const savePost = (auth, blogName, post) => { 'User-Agent': appInfo.userAgent } }) - .then(errorHandler) + .then(_errorHandler) .then(res => fetchPost(auth, blogName, res.tistory.postId)) } -const addPost = (auth, blogName, post) => { +function addPost(auth, blogName, post) { let formdata = makePostFormData(auth, blogName, post) return fetch(BASE_URL + "/post/write", { @@ -166,11 +186,11 @@ const addPost = (auth, blogName, post) => { 'User-Agent': appInfo.userAgent } }) - .then(errorHandler) + .then(_errorHandler) .then(res => fetchPost(auth, blogName, res.tistory.postId)) } -const makePostFormData = (auth, blogName, post) => { +function makePostFormData(auth, blogName, post) { let formdata = new FormData() formdata.append("access_token", auth.access_token) formdata.append("output", "json") @@ -189,19 +209,19 @@ const makePostFormData = (auth, blogName, post) => { return formdata } -const uploadFile = (auth, blogName, filepath) => { +function uploadFile(auth, blogName, filepath) { console.log("uploadFile", blogName, filepath) return _uploadFile(auth.access_token, blogName, fs.createReadStream(filepath)) } -const uploadFileWithBuffer = (auth, blogName, buffer, options) => { +function uploadFileWithBuffer(auth, blogName, buffer, options) { console.log("uploadFileWithClipboard", blogName) var imageStream = new stream.PassThrough() imageStream.end(buffer) return _uploadFile(auth.access_token, blogName, imageStream, options) } -const uploadFileWithBlob = (auth, blogName, blob, options) => { +function uploadFileWithBlob(auth, blogName, blob, options) { console.log("uploadFileWithBlob", blogName) var imageStream = new stream.PassThrough() imageStream.end(blob.buffer) @@ -212,24 +232,12 @@ const uploadFileWithBlob = (auth, blogName, blob, options) => { }) } -const _uploadFile = (accessToken, blogName, fileBlob, fileOption) => { - let formdata = new FormData(); - formdata.append("access_token", accessToken) - formdata.append("output", "json") - formdata.append("blogName", blogName) - formdata.append("uploadedfile", fileBlob, fileOption) - return fetch(BASE_URL + "/post/attach", { - method: 'post', - body: formdata - }) - .then(errorHandler) - .then(res => res.tistory.url) +function validateAuthInfo(auth) { + return auth && auth.access_token } -const validateAuthInfo = (auth) => auth && auth.access_token - -const fetchAccount = async (auth) => { +async function fetchAccount(auth) { let user = { name: "tistory", image: null diff --git a/src/main/apis/tumblr-api.js b/src/main/apis/tumblr-api.js index f5e5da1..19a4940 100644 --- a/src/main/apis/tumblr-api.js +++ b/src/main/apis/tumblr-api.js @@ -6,32 +6,7 @@ const OAuthRequestManager = require('../oauth/OAuthRequestManager'); const PROVIDER_ID = 'tumblr' -const requestAuth = (successHandler, failureHandler) => { - const oauthInfoReader = new OauthInfoReader() - const oauth1 = new ExternalOAuth1(oauthInfoReader.getTumblr()) - oauth1.requestAuth((requestTokens) => { - OAuthRequestManager.saveRequestInfo('oauth', (searchParams) => { - const verifier = searchParams.get("oauth_verifier") - oauth1.requestToken(verifier, requestTokens, (error, token, tokenSecret) => { - if (error) { - failureHandler() - return - } - - successHandler({ - uuid: uuid(), - provider: PROVIDER_ID, - authInfo: { - token, tokenSecret - } - }) - }) - }) - }) - -} - -function createTumblrClient(auth) { +function _createTumblrClient(auth) { const oauthReader = new OauthInfoReader() const tumblrInfo = oauthReader.getTumblr() @@ -46,10 +21,6 @@ function createTumblrClient(auth) { }) } -const fetchUser = (auth) => { - const client = createTumblrClient(auth) - return client.userInfo() -} function _tumblrPostToEditorPost(post) { return ({ @@ -85,8 +56,39 @@ function _editorPostToTumblrPost(editorPost) { return tumblrPost } -const fetchPosts = (auth, blogName, options) => { - const client = createTumblrClient(auth) +function requestAuth(successHandler, failureHandler) { + const oauthInfoReader = new OauthInfoReader() + const oauth1 = new ExternalOAuth1(oauthInfoReader.getTumblr()) + oauth1.requestAuth((requestTokens) => { + OAuthRequestManager.saveRequestInfo('oauth', (searchParams) => { + const verifier = searchParams.get("oauth_verifier") + oauth1.requestToken(verifier, requestTokens, (error, token, tokenSecret) => { + if (error) { + failureHandler() + return + } + + successHandler({ + uuid: uuid(), + provider: PROVIDER_ID, + authInfo: { + token, tokenSecret + } + }) + }) + }) + }) + +} + +function fetchUser(auth) { + const client = _createTumblrClient(auth) + return client.userInfo() +} + + +function fetchPosts(auth, blogName, options) { + const client = _createTumblrClient(auth) return client.blogPosts(blogName, options) .then(res => ({ posts: _tumblrPostsToEditorPosts(res.posts), @@ -94,30 +96,32 @@ const fetchPosts = (auth, blogName, options) => { })) } -const fetchPost = (auth, blogName, postId) => { - const client = createTumblrClient(auth) +function fetchPost(auth, blogName, postId) { + const client = _createTumblrClient(auth) return client.blogPosts(blogName, {id: postId}) .then(res => ({ post: _tumblrPostToEditorPost(res.posts[0]) })) } -const addPost = async (auth, blogName, post) => { - const client = createTumblrClient(auth) +async function addPost(auth, blogName, post) { + const client = _createTumblrClient(auth) const res = await client.createTextPost(blogName, _editorPostToTumblrPost(post)) const fetchRes = await fetchPosts(auth, blogName, {offset:0, limit:1}) return { post: fetchRes.posts[0] } } -const savePost = async (auth, blogName, post) => { - const client = createTumblrClient(auth) +async function savePost(auth, blogName, post) { + const client = _createTumblrClient(auth) const res = await client.editPost(blogName, _editorPostToTumblrPost(post)) return await fetchPost(auth, blogName, post.id) } -const validateAuthInfo = (auth) => auth && auth.token +function validateAuthInfo(auth) { + return auth && auth.token +} -const fetchAccount = async (auth) => { +async function fetchAccount(auth) { let blogs = [] let username = "" try { diff --git a/src/renderer/components/editor/tinymce/plugins/file-upload/plugin.js b/src/renderer/components/editor/tinymce/plugins/file-upload/plugin.js index dcd41d0..2932017 100644 --- a/src/renderer/components/editor/tinymce/plugins/file-upload/plugin.js +++ b/src/renderer/components/editor/tinymce/plugins/file-upload/plugin.js @@ -1,8 +1,4 @@ -import React from 'react' -import { unmountComponentAtNode } from 'react-dom' -import autobind from 'autobind-decorator' - -const plugin = function (editor) { +export default function plugin(editor) { editor.options.register("open_file_handler", { processor: 'function', default: () => {} @@ -23,5 +19,3 @@ const plugin = function (editor) { } }) } - -export default plugin diff --git a/src/renderer/index.js b/src/renderer/index.js index d9b7bbe..43cc27f 100644 --- a/src/renderer/index.js +++ b/src/renderer/index.js @@ -1,5 +1,4 @@ import React from 'react' -import { render } from 'react-dom' import { createRoot } from 'react-dom/client' import { Provider } from 'react-redux' diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index d197c39..0000000 --- a/tsconfig.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "compilerOptions": { - "experimentalDecorators": true, - "allowJs": true, - "noEmit": true - } -}