mirror of https://github.com/laurent22/joplin.git
200 lines
5.7 KiB
JavaScript
200 lines
5.7 KiB
JavaScript
const { netUtils } = require('lib/net-utils');
|
|
const urlParser = require("url");
|
|
const Note = require('lib/models/Note');
|
|
const Folder = require('lib/models/Folder');
|
|
const Resource = require('lib/models/Resource');
|
|
const Setting = require('lib/models/Setting');
|
|
const { shim } = require('lib/shim');
|
|
const md5 = require('md5');
|
|
const { fileExtension, safeFileExtension, filename } = require('lib/path-utils');
|
|
const HtmlToMd = require('lib/HtmlToMd');
|
|
const { Logger } = require('lib/logger.js');
|
|
|
|
class ClipperServer {
|
|
|
|
constructor() {
|
|
this.logger_ = new Logger();
|
|
}
|
|
|
|
setLogger(l) {
|
|
this.logger_ = l;
|
|
}
|
|
|
|
logger() {
|
|
return this.logger_;
|
|
}
|
|
|
|
htmlToMdParser() {
|
|
if (this.htmlToMdParser_) return this.htmlToMdParser_;
|
|
this.htmlToMdParser_ = new HtmlToMd();
|
|
return this.htmlToMdParser_;
|
|
}
|
|
|
|
async requestNoteToNote(requestNote) {
|
|
const output = {
|
|
title: requestNote.title ? requestNote.title : '',
|
|
body: requestNote.body ? requestNote.body : '',
|
|
};
|
|
|
|
if (requestNote.bodyHtml) {
|
|
console.info(requestNote.bodyHtml);
|
|
|
|
// Parsing will not work if the HTML is not wrapped in a top level tag, which is not guaranteed
|
|
// when getting the content from elsewhere. So here wrap it - it won't change anything to the final
|
|
// rendering but it makes sure everything will be parsed.
|
|
output.body = await this.htmlToMdParser().parse('<div>' + requestNote.bodyHtml + '</div>', {
|
|
baseUrl: requestNote.baseUrl ? requestNote.baseUrl : '',
|
|
});
|
|
}
|
|
|
|
if (requestNote.parent_id) {
|
|
output.parent_id = requestNote.parent_id;
|
|
} else {
|
|
const folder = await Folder.defaultFolder();
|
|
if (!folder) throw new Error('Cannot find folder for note');
|
|
output.parent_id = folder.id;
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
extractImageUrls_(md) {
|
|
// ![some text](http://path/to/image)
|
|
const regex = new RegExp(/!\[.*?\]\((http[s]?:\/\/.*?)\)/, 'g')
|
|
let match = regex.exec(md);
|
|
const output = [];
|
|
while (match) {
|
|
const url = match[1];
|
|
if (output.indexOf(url) < 0) output.push(url);
|
|
match = regex.exec(md);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
async downloadImages_(urls) {
|
|
const tempDir = Setting.value('tempDir');
|
|
const output = {};
|
|
|
|
for (let i = 0; i < urls.length; i++) {
|
|
const url = urls[i];
|
|
const name = filename(url);
|
|
let fileExt = safeFileExtension(fileExtension(url).toLowerCase());
|
|
if (fileExt) fileExt = '.' + fileExt;
|
|
let imagePath = tempDir + '/' + name + fileExt;
|
|
if (await shim.fsDriver().exists(imagePath)) imagePath = tempDir + '/' + name + '_' + md5(Math.random() + '_' + Date.now()).substr(0,10) + fileExt;
|
|
|
|
try {
|
|
const result = await shim.fetchBlob(url, { path: imagePath });
|
|
output[url] = { path: imagePath };
|
|
} catch (error) {
|
|
this.logger().warn('ClipperServer: Cannot download image at ' + url, error);
|
|
}
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
async createResourcesFromPaths_(urls) {
|
|
for (let url in urls) {
|
|
if (!urls.hasOwnProperty(url)) continue;
|
|
const urlInfo = urls[url];
|
|
try {
|
|
const resource = await shim.createResourceFromPath(urlInfo.path);
|
|
urlInfo.resource = resource;
|
|
} catch (error) {
|
|
this.logger().warn('ClipperServer: Cannot create resource for ' + url, error);
|
|
}
|
|
}
|
|
return urls;
|
|
}
|
|
|
|
replaceImageUrlsByResources_(md, urls) {
|
|
let output = md.replace(/(!\[.*?\]\()(http[s]?:\/\/.*?)(\))/g, (match, before, imageUrl, after) => {
|
|
const urlInfo = urls[imageUrl];
|
|
if (!urlInfo || !urlInfo.resource) return imageUrl;
|
|
const resourceUrl = Resource.internalUrl(urlInfo.resource);
|
|
return before + resourceUrl + after;
|
|
});
|
|
|
|
return output;
|
|
}
|
|
|
|
async start() {
|
|
const port = await netUtils.findAvailablePort([9967, 8967, 8867], 0); // TODO: Make it shared with OneDrive server
|
|
if (!port) throw new Error('All potential ports are in use or not available.');
|
|
|
|
const server = require('http').createServer();
|
|
|
|
server.on('request', (request, response) => {
|
|
|
|
const writeCorsHeaders = (code) => {
|
|
response.writeHead(code, {
|
|
"Content-Type": "application/json",
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
|
|
'Access-Control-Allow-Headers': 'X-Requested-With,content-type',
|
|
});
|
|
}
|
|
|
|
const writeResponseJson = (code, object) => {
|
|
writeCorsHeaders(code);
|
|
response.write(JSON.stringify(object));
|
|
response.end();
|
|
}
|
|
|
|
console.info('GOT REQUEST', request.method + ' ' + request.url);
|
|
|
|
if (request.method === 'POST') {
|
|
const url = urlParser.parse(request.url, true);
|
|
|
|
if (url.pathname === '/notes') {
|
|
let body = '';
|
|
|
|
request.on('data', (data) => {
|
|
body += data;
|
|
});
|
|
|
|
request.on('end', async () => {
|
|
try {
|
|
const requestNote = JSON.parse(body);
|
|
let note = await this.requestNoteToNote(requestNote);
|
|
|
|
// TODO: Provide way to check status (importing image x/y)
|
|
// TODO: Delete temp file after import
|
|
// TODO: Download multiple images at once
|
|
|
|
const imageUrls = this.extractImageUrls_(note.body);
|
|
let result = await this.downloadImages_(imageUrls);
|
|
result = await this.createResourcesFromPaths_(result);
|
|
note.body = this.replaceImageUrlsByResources_(note.body, result);
|
|
|
|
note = await Note.save(note);
|
|
return writeResponseJson(200, note);
|
|
} catch (error) {
|
|
console.warn(error);
|
|
return writeResponseJson(400, { errorCode: 'exception', errorMessage: error.message });
|
|
}
|
|
});
|
|
} else {
|
|
return writeResponseJson(404, { errorCode: 'not_found' });
|
|
}
|
|
} else if (request.method === 'OPTIONS') {
|
|
writeCorsHeaders(200);
|
|
response.end();
|
|
} else {
|
|
return writeResponseJson(405, { errorCode: 'method_not_allowed' });
|
|
}
|
|
});
|
|
|
|
server.on('close', () => {
|
|
|
|
});
|
|
|
|
console.info('Starting Clipper server on port ' + port);
|
|
|
|
server.listen(port);
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = ClipperServer; |