let db = require('@config/database') let SiteScrape = require('@helpers/SiteScrape') const cs = require('@helpers/CryptoString') let Attachment = module.exports = {} const cheerio = require('cheerio') const fs = require('fs') const path = require('path') const crypto = require('crypto') const gm = require('gm') const tesseract = require("node-tesseract-ocr") // Left as-is deliberately: the upload / OCR / thumbnail code below resolves // against it, and it is correct under pm2 (cwd = server/). Not touching it // keeps the upload path byte-identical. const filePath = '../staticFiles/' // Absolute equivalent, used by the scrape path only. filePath is CWD-relative, // so under Jest (cwd = repo root) scrape downloads landed in a sibling of the // repo rather than in staticFiles/. This resolves to the same directory as // filePath under pm2. const scrapeFilePath = path.join(__dirname, '..', '..', 'staticFiles') + path.sep const config = require('@config/scrape') const UrlSafety = require('@helpers/UrlSafety') const HttpFetch = require('@helpers/HttpFetch') const Realtime = require('@helpers/Realtime') // Attachment.migrateOld Attachment.textSearch = (userId, searchTerm) => { return new Promise((resolve, reject) => { const front = 5 const tail = 150 const query = ` SELECT *, substring( text, IF(LOCATE(?, text) > ${tail}, LOCATE(?, text) - ${front}, 1), ${tail} + LENGTH(?) + ${front} ) as snippet FROM attachment WHERE user_id = ? AND visible != 0 AND MATCH(text) AGAINST(? IN NATURAL LANGUAGE MODE) LIMIT 1000` db.promise() .query(query, [searchTerm, searchTerm, searchTerm, userId, searchTerm]) .then((rows, fields) => { resolve(rows[0]) //Return all attachments found by query }) .catch(console.log) }) } Attachment.search = (userId, noteId, attachmentType, offset, setSize, includeShared) => { console.log([userId, noteId, attachmentType, offset, setSize, includeShared]) return new Promise((resolve, reject) => { let params = [userId] let query = ` SELECT attachment.*, note.share_user_id FROM attachment LEFT JOIN note ON (attachment.note_id = note.id) WHERE attachment.user_id = ? AND visible = 1 ` if(noteId && noteId > 0){ // // Show everything if note ID is present // query += 'AND attachment.note_id = ? ' params.push(noteId) } else { // // Other filters if NO note id // if(attachmentType == 'links'){ query += 'AND attachment_type = 1 ' } if(attachmentType == 'files'){ query += 'AND attachment_type > 1 ' } query += `AND note.archived = ${ attachmentType == 'archived' ? '1':'0' } ` query += `AND note.trashed = ${ attachmentType == 'trashed' ? '1':'0' } ` if(!attachmentType){ // Null note ID means it was pushed by bookmarklet query += 'OR attachment.note_id IS NULL ' } } if(!noteId){ const sharedOrNot = includeShared ? ' NOT ':' ' query += `AND note.share_user_id IS${sharedOrNot}NULL ` } query += 'ORDER BY last_indexed DESC ' const limitOffset = parseInt(offset, 10) || 0 //Either parse int, or use zero const parsedSetSize = parseInt(setSize, 10) || 20 query += ` LIMIT ${limitOffset}, ${parsedSetSize}` console.log(query) db.promise() .query(query, params) .then((rows, fields) => { resolve(rows[0]) //Return all attachments found by query }) .catch(console.log) }) } Attachment.urlForNote = (userId, noteId) => { return new Promise((resolve, reject) => { db.promise() .query(`SELECT * FROM attachment WHERE user_id = ? AND note_id = ? AND attachment_type = 1 ORDER BY last_indexed DESC;`, [userId, noteId]) .then((rows, fields) => { resolve(rows[0]) //Return all attachments found by query }) .catch(console.log) }) } //Update attachment in database Attachment.update = (userId, attachmentId, updatedText, noteId) => { return new Promise((resolve, reject) => { db.promise() .query(`UPDATE attachment SET text = ? WHERE id = ? AND user_id = ?`, [updatedText, attachmentId, userId]) .then((rows, fields) => { resolve(true) }) .catch(console.log) }) } Attachment.delete = (userId, attachmentId, urlDelete = false) => { let attachment = null let noteExists = true return new Promise((resolve, reject) => { db.promise() .query('SELECT * FROM attachment WHERE id = ? AND user_id = ? LIMIT 1', [attachmentId, userId]) .then((rows, fields) => { //Attachment doesn't exist, return done if(rows[0].length == 0){ return resolve(true) } attachment = rows[0][0] return db.promise().query('SELECT count(id) as `exists` FROM note WHERE id = ?', [attachment.note_id]) }) .then((rows, fields) => { noteExists = (rows[0][0]['exists'] > 0) let url = attachment.url const noteId = attachment.note_id //Try to delete file and thumbnail try { fs.unlinkSync(filePath+attachment.file_location) } catch(err) { console.error('File Does not exist') } try { fs.unlinkSync(filePath+'thumb_'+attachment.file_location) } catch(err) { console.error('Thumbnail Does not exist') } //Do not delete link attachments, just hide them. They will be deleted if removed from note or if note is deleted if(attachment.attachment_type == 1 && !urlDelete && noteExists){ db.promise() .query(`UPDATE attachment SET visible = 0 WHERE id = ?`, [attachmentId]) .then((rows, fields) => resolve(true)) .catch(console.log) return resolve(true) } else { db.promise() .query(`DELETE FROM attachment WHERE id = ?`, [attachmentId]) .then((rows, fields) => resolve(true)) .catch(console.log) } }) .catch(console.log) }) } Attachment.processUploadedFile = (userId, noteId, fileObject) => { return new Promise((resolve, reject) => { const rawFilename = fileObject.filename const extension = '.'+fileObject.originalname.split('.').pop() const goodFileName = rawFilename+extension const fileName = fileObject.originalname //Actual name of the file, dog.jpg //Rename random file name to one with an extension fs.rename(filePath+rawFilename, filePath+goodFileName, (err) => { const created = Math.round((+new Date)/1000) db.promise() .query(` INSERT INTO attachment (note_id, user_id, attachment_type, \`text\`, last_indexed, file_location) VALUES (?, ?, ?, ?, ?, ?) `, [noteId, userId, 2, 'Add a description to -> '+fileName, created, goodFileName]) .then((rows, fields) => { Attachment.generateThumbnail(goodFileName) //If its an image, scrape text if(true){ // https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality //psm 3 - default, 11 - as much text as possible const config = { lang: "eng", oem: 1, psm: 3 } tesseract.recognize(filePath+goodFileName, config) .then(text => { text = text.slice(0, -1).trim() if(text.length > 5){ console.log('Inserting text') db.promise().query( `UPDATE attachment SET text = ? WHERE id = ? AND user_id = ? LIMIT 1`, [text, rows[0].insertId, userId] ).then(results => { resolve({ fileName, goodFileName }) }) } else { return resolve({ fileName, goodFileName }) } }) .catch(error => { console.log(error.message) }) } else { resolve({ fileName, goodFileName }) } }) .catch(console.log) }) }) } Attachment.generateThumbnail = (fileName) => { return new Promise((resolve, reject) => { gm(filePath+fileName) .resize(550) //Resize to width of 550 px .quality(75) //compression level 0 - 100 (best) .write(filePath + 'thumb_'+fileName, function (err) { resolve(fileName) }) }) } // // Diff the URLs currently in a note against the attachments already stored for // it, soft-delete the ones that vanished, and enqueue the new ones. // // Returns immediately with the text already known from cache; freshly scraped // text arrives asynchronously via the worker. Nothing here waits on the network. // Attachment.scanTextForWebsites = (userId, noteId, noteText) => { const ScrapeQueue = require('@workers/ScrapeQueue') const Schema = require('@helpers/Schema') // Fixed: the old version called resolve() without returning, so an empty // note fell through and ran the DB lookup anyway. if (!noteText || noteText.length == 0) { return Promise.resolve('') } // When the scrape schema is absent the new columns do not exist, so // creating a pending row would fail on every save. The diff and soft-delete // below still run - they touch no new columns - so removing a link from a // note behaves exactly as before. const scrapeAvailable = Schema.isScrapeSchemaReady() let knownText = '' return Attachment.urlForNote(userId, noteId) .then(attachments => { const foundUrls = SiteScrape.getCleanUrls(noteText) const remaining = foundUrls.slice() attachments.forEach(attachment => { // Compared on normalised keys rather than raw strings, so an // http/https or www. variant is not treated as a different // resource and needlessly deleted and re-scraped. const index = remaining.findIndex(url => url === attachment.url || UrlSafety.sameResource(url, attachment.url)) if (index !== -1) { knownText += attachment.text || '' remaining.splice(index, 1) } else { // Unchanged behaviour: link attachments are hidden, not deleted. Attachment.delete(userId, attachment.id, true) } }) if (!remaining.length) { return knownText } // No schema, no previews. Return what is already cached rather than // attempting inserts that cannot succeed. if (!scrapeAvailable) { return knownText } // Create a shell row per new URL, then hand off to the worker. return Promise.all(remaining.map(url => Attachment.createPendingUrlAttachment(userId, noteId, url) .then(attachmentId => ({ userId, noteId, attachmentId, url })) .catch(error => { console.log('[scrape] could not create attachment row', url, error.message) return null }) )) .then(jobs => { const valid = jobs.filter(Boolean) // Fire and forget - the note save must not wait on this. ScrapeQueue.enqueueUrls(valid) .catch(error => console.log('[scrape] enqueue failed', error.message)) return knownText }) }) // The old version had no catch here at all. .catch(error => { console.log('[scrape] scanTextForWebsites failed', error.message) return knownText }) } // // Create the shell attachment row for a URL and return its id. // // Returns the id BEFORE any network work begins. The old code armed a 5s // setTimeout calling request.cancel() synchronously, before this INSERT had // resolved - so a slow INSERT meant the timer fired against a null reference // and threw inside a bare timer callback, taking the process down. // Attachment.createPendingUrlAttachment = (userId, noteId, url) => { const created = Math.round((+new Date) / 1000) return db.promise() .query( `INSERT INTO attachment (note_id, user_id, attachment_type, text, url, last_indexed, file_location, scrape_state, enqueued_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, [noteId, userId, 1, url, url, created, null, 0, created] ) .then(([result]) => result.insertId) } // // Scrape a set of URLs for a note and resolve with their combined text. // // Kept for the bookmarklet path. The old implementation resolved only when a // completion counter reached foundUrls.length, so a single rejection meant the // promise never settled and the caller hung forever. allSettled inside the // worker removes the counter entirely. // Attachment.scrapeUrlsCreateAttachments = (userId, noteId, foundUrls) => { const ScrapeQueue = require('@workers/ScrapeQueue') const Schema = require('@helpers/Schema') if (!foundUrls || !foundUrls.length) { return Promise.resolve('') } if (!Schema.isScrapeSchemaReady()) { return Promise.resolve('') } return Promise.all(foundUrls.map(url => Attachment.createPendingUrlAttachment(userId, noteId, url) .then(attachmentId => ({ userId, noteId, attachmentId, url })) .catch(error => { console.log('[scrape] could not create attachment row', url, error.message) return null }) )) .then(jobs => ScrapeQueue.enqueueUrls(jobs.filter(Boolean))) .then(texts => texts.join('')) } // // Download and store a preview image. // // The old downloadFileFromUrl had no timeout, no size limit, no User-Agent and // no status check: it piped whatever came back straight into a write stream, so // a 404 HTML error page was written to disk and handed to gm. It also wrote the // file before validating anything, which is why staticFiles/ accumulated // zero-byte orphans. // Attachment.downloadThumbnail = (imageUrl, pageUrl) => { if (!imageUrl) { return Promise.resolve(null) } // crypto.randomBytes rather than two concatenated Math.random() calls. const fileName = crypto.randomBytes(16).toString('hex') + '_scrape' const fullPath = scrapeFilePath + fileName const thumbPath = scrapeFilePath + 'thumb_' + fileName const tempPath = fullPath + '.tmp' return HttpFetch.fetchBinary(imageUrl, { pageUrl, timeoutMs: config.imageTimeoutMs, maxBytes: config.maxImageBytes, }) .then(result => { // Status and content-type were both gated inside fetchBinary, so by // here we are holding bytes that really are an image. if (!result.buffer || !result.buffer.length) { return null } if (!magicBytesMatch(result.buffer, result.mime)) { console.log('[scrape] image bytes do not match declared type', result.mime, imageUrl) return null } // Write to a temp path, then rename, so a failure cannot leave a // half-written file behind for the client to serve. return fs.promises.writeFile(tempPath, result.buffer) .then(() => fs.promises.rename(tempPath, fullPath)) .then(() => resizeToThumbnail(fullPath, thumbPath)) .then(() => fileName) .catch(error => { console.log('[scrape] could not store image', imageUrl, error.message) return cleanupFiles([tempPath, fullPath, thumbPath]).then(() => null) }) }) .catch(error => { console.log('[scrape] image fetch failed', imageUrl, error.kind || error.message) return null }) } // // Resize via gm. Degrades rather than failing: gm (and tesseract) are not in // shell.nix buildInputs, so the binary is absent in dev - the old code returned // null here and left the downloaded file orphaned on disk. Copying the // full-size image to the thumbnail path keeps the preview working. // const resizeToThumbnail = (fullPath, thumbPath) => { return new Promise(resolve => { gm(fullPath) .resize(550) //Resize to width of 550 px .quality(85) //compression level 0 - 100 (best) .write(thumbPath, error => { if (!error) { return resolve(true) } console.log('[scrape] gm unavailable or failed, using full size as thumbnail') fs.promises.copyFile(fullPath, thumbPath) .then(() => resolve(true)) .catch(() => resolve(false)) }) }) } const cleanupFiles = (paths) => { return Promise.all(paths.map(target => fs.promises.unlink(target).catch(() => null) )) } // // Sanity-check the bytes against the declared content type. A server can claim // image/png and send anything. // const IMAGE_MAGIC = [ { mime: 'image/png', bytes: [0x89, 0x50, 0x4e, 0x47] }, { mime: 'image/jpeg', bytes: [0xff, 0xd8, 0xff] }, { mime: 'image/gif', bytes: [0x47, 0x49, 0x46, 0x38] }, { mime: 'image/bmp', bytes: [0x42, 0x4d] }, ] const magicBytesMatch = (buffer, mime) => { const expected = IMAGE_MAGIC.find(entry => entry.mime === mime) // webp/avif/ico and friends have container-dependent headers - accept them // on the content-type alone rather than guessing wrong. if (!expected) { return true } if (buffer.length < expected.bytes.length) { return false } return expected.bytes.every((byte, index) => buffer[index] === byte) } Attachment.generatePushKey = (userId) => { return new Promise((resolve, reject) => { db.promise() .query("SELECT pushkey FROM user WHERE id = ? LIMIT 1", [userId]) .then((rows, fields) => { const pushKey = rows[0][0].pushkey // push key exists if(pushKey && pushKey.length > 0){ return resolve(pushKey) } else { // generate and save a new key const newPushKey = cs.createSmallSalt() db.promise() .query('UPDATE user SET pushkey = ? WHERE id = ? LIMIT 1', [newPushKey,userId]) .then((rows, fields) => { return resolve(newPushKey) }) } }) }) } Attachment.deletePushKey = (userId) => { return new Promise((resolve, reject) => { db.promise() .query('UPDATE user SET pushkey = null WHERE id = ? LIMIT 1', [userId]) .then((rows, fields) => { return resolve(rows[0].affectedRows == 1) }) }) } Attachment.getPushkeyBookmarklet = (userId) => { return new Promise((resolve, reject) => { Attachment.generatePushKey(userId) .then( pushKey => { let bookmarklet = Attachment.generateBookmarkletText(pushKey) return resolve(bookmarklet) }) }) } // // Bookmarklet entry point: GET /api/public/pushmebaby?pushkey=...&url=... // // The URL arrives straight off a query string, so it is validated here - the // old code passed it through completely unvalidated, bypassing even the https // normalisation that note text goes through. // // Response timing is preserved: the bookmarklet still answers after the scrape, // but now with a hard cap instead of the previous unbounded hang. // Attachment.pushUrl = (pushkey, url) => { const ScrapeQueue = require('@workers/ScrapeQueue') const Schema = require('@helpers/Schema') // Preserve the bookmarklet contract - resolve truthy, leaking nothing - // rather than failing an insert against columns that do not exist. if (!Schema.isScrapeSchemaReady()) { return Promise.resolve(true) } let userId = null pushkey = String(pushkey || '').replace(/ /g, '+') return db.promise() .query("SELECT id FROM user WHERE pushkey = ? LIMIT 1", [pushkey]) .then(([rows]) => { // Unchanged: an unknown pushkey resolves truthy and leaks nothing // about whether the key exists. if (rows.length == 0) { return null } userId = rows[0].id return UrlSafety.assertFetchable(url) .then(() => Attachment.createPendingUrlAttachment(userId, null, url)) .then(attachmentId => ScrapeQueue.awaitUrl( ScrapeQueue.enqueueUrl({ userId, noteId: null, attachmentId, url }), config.totalBudgetMs )) .catch(error => { console.log('[scrape] pushUrl refused', url, error.reason || error.message) return null }) }) .then(() => { if (userId) { Realtime.emitToUser(userId, 'update_counts') Realtime.emitToUser(userId, 'update_note_attachments') } return true }) .catch(error => { console.log('[scrape] pushUrl failed', error.message) return true }) } Attachment.generateBookmarkletText = (pushKey) => { const endpoint = '/api/public/pushmebaby' let url = 'https://www.solidscribe.com' + endpoint if(process.env.NODE_ENV === 'development'){ // url = 'https://192.168.1.164' + endpoint } // Terminate each line with a semi-colon, super important, since spaces are removed. // document.getElementById(id).remove(); url += '?pushkey='+encodeURIComponent(pushKey) const bookmarkletV3 = ` javascript: (() => { var p = encodeURIComponent(window.location.href); var n = "`+url+`&url="+p; window.open(n, '_blank', 'noopener=noopener'); window.focus(); var k = document.createElement("div"); k.setAttribute("style", "position:fixed;right:10px;top:10px;z-index:222222;border-radius:4px;font-size:1.3em;padding:20px 15px;background: #8f51be;color:white;"); k.innerHTML = "Posted URL to your Solid Scribe account"; document.body.appendChild(k); setTimeout(()=>{ k.remove(); },5000); })(); ` return bookmarkletV3 .replace(/\t|\r|\n/gm, "") // Remove tabs, new lines, returns .replace(/\s+/g, ' ') // remove double spaces .trim() }