Remove TinyMce Added Squire
This commit is contained in:
@@ -8,7 +8,7 @@ ProcessText.removeHtml = (string) => {
|
||||
}
|
||||
|
||||
return string
|
||||
.replace(/&[#A-Za-z0-9]+;/g,' ') //Rip out all HTML entities
|
||||
.replace(/&[[#A-Za-z0-9]+A-Za-z0-9]+;/g,' ') //Rip out all HTML entities
|
||||
.replace(/<[^>]+>/g, ' ') //Rip out all HTML tags
|
||||
.replace(/\s+/g, ' ') //Remove all whitespace
|
||||
.trim()
|
||||
@@ -25,6 +25,8 @@ ProcessText.getUrlsFromString = (string) => {
|
||||
+ Empty lines are skipped
|
||||
+ URLs are turned into links
|
||||
+ All URLs are givent the target="_blank" property
|
||||
+ Lists are given extra display characters
|
||||
+ If note starts as a list, skip the title
|
||||
*/
|
||||
|
||||
ProcessText.deduceNoteTitle = (inString) => {
|
||||
@@ -36,7 +38,16 @@ ProcessText.deduceNoteTitle = (inString) => {
|
||||
return {title, sub}
|
||||
}
|
||||
|
||||
let lines = inString.match(/[^\r\n]+/g)
|
||||
//Remove inline styles that may be added by editor
|
||||
inString = inString.replace(/style=".*?"/g,'')
|
||||
|
||||
//Match full line and closing tag or just closing tag
|
||||
let lines = inString.match(/[<[a-zA-Z0-9]+>(.*?)<\/[a-zA-Z0-9]+>|<\/[a-zA-Z0-9>]+?>/g)
|
||||
if(lines == null){ lines = [inString] }
|
||||
//.match(/[^\r\n]+/g) //Match return or newline
|
||||
|
||||
// console.log(lines)
|
||||
|
||||
let finalLines = []
|
||||
|
||||
const startTags = ['<ol','<li','<ul']
|
||||
@@ -45,6 +56,7 @@ ProcessText.deduceNoteTitle = (inString) => {
|
||||
let totalLines = Math.min(lines.length, 6)
|
||||
let charLimit = 250
|
||||
let listStart = false
|
||||
let noTitleJustList = false
|
||||
|
||||
for(let i=0; i < totalLines; i++){
|
||||
|
||||
@@ -53,7 +65,10 @@ ProcessText.deduceNoteTitle = (inString) => {
|
||||
continue
|
||||
}
|
||||
|
||||
const cleanLine = ProcessText.removeHtml(lines[i]).trim().replace(' ','')
|
||||
//Various empty chars are possible
|
||||
const cleanLine = ProcessText.removeHtml(lines[i])
|
||||
.replace('<br>','')
|
||||
.trim()
|
||||
const lineStart = lines[i].trim().substring(0, 3)
|
||||
charLimit -= cleanLine.length
|
||||
|
||||
@@ -69,6 +84,11 @@ ProcessText.deduceNoteTitle = (inString) => {
|
||||
continue
|
||||
}
|
||||
|
||||
//Check if note starts with a list, don't include title, just show list
|
||||
if(finalLines.length == 0 && startTags.includes(lineStart)){
|
||||
noTitleJustList = true
|
||||
}
|
||||
|
||||
//Empty line, may be a list open or close
|
||||
if(cleanLine.length == 0 && (startTags.includes(lineStart) || endTags.includes(lineStart) )){
|
||||
if(listStart == false){
|
||||
@@ -86,7 +106,7 @@ ProcessText.deduceNoteTitle = (inString) => {
|
||||
}
|
||||
|
||||
//Skip empty lines
|
||||
if(!cleanLine || cleanLine.length == 0 || cleanLine == ' '){
|
||||
if(!cleanLine || cleanLine.length == 0){
|
||||
totalLines++
|
||||
continue
|
||||
}
|
||||
@@ -126,7 +146,7 @@ ProcessText.deduceNoteTitle = (inString) => {
|
||||
}
|
||||
|
||||
//Pull out title if its not an empty string
|
||||
if(ProcessText.removeHtml(finalLines[0]).trim().replace(' ','').length > 0){
|
||||
if(ProcessText.removeHtml(finalLines[0]).trim().replace(' ','').length > 0 && !noTitleJustList){
|
||||
title = finalLines.shift()
|
||||
}
|
||||
|
||||
|
@@ -7,6 +7,70 @@ const express = require('express')
|
||||
const app = express()
|
||||
const port = 3000
|
||||
|
||||
var http = require('http').createServer(app);
|
||||
var io = require('socket.io')(http, {
|
||||
path:'/socket'
|
||||
});
|
||||
|
||||
// Make io accessible to our router
|
||||
app.use(function(req,res,next){
|
||||
req.io = io;
|
||||
next();
|
||||
});
|
||||
|
||||
io.on('connection', function(socket){
|
||||
|
||||
// console.log('New user ', socket.id)
|
||||
|
||||
socket.on('join_room', roomId => {
|
||||
// console.log('Join room ', roomId)
|
||||
socket.join(roomId)
|
||||
|
||||
const usersInRoom = io.sockets.adapter.rooms[roomId]
|
||||
if(usersInRoom){
|
||||
// console.log('Users in room', usersInRoom.length)
|
||||
io.to(roomId).emit('update_user_count', usersInRoom.length)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on('leave_room', roomId => {
|
||||
socket.leave(roomId)
|
||||
// console.log('User Left room')
|
||||
|
||||
const usersInRoom = io.sockets.adapter.rooms[roomId]
|
||||
if(usersInRoom){
|
||||
// console.log('Users in room', usersInRoom.length)
|
||||
io.to(roomId).emit('update_user_count', usersInRoom.length)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
socket.on('note_diff', data => {
|
||||
|
||||
//Each user joins a room when they open the app.
|
||||
io.in(data.id).clients((error, clients) => {
|
||||
if (error) throw error;
|
||||
//Go through each client in note room and send them the diff
|
||||
clients.forEach(socketId => {
|
||||
if(socketId != socket.id){
|
||||
io.to(socketId).emit('incoming_diff', data.diff)
|
||||
}
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
socket.on('disconnect', function(){
|
||||
// console.log('user disconnected');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
http.listen(3001, function(){
|
||||
console.log('socket.io liseting on port 3001');
|
||||
});
|
||||
|
||||
//Enable json body parsing in requests. Allows me to post data in ajax calls
|
||||
app.use(express.json({limit: '2mb'}))
|
||||
|
||||
|
@@ -7,13 +7,47 @@ const rp = require('request-promise')
|
||||
const request = require('request')
|
||||
const fs = require('fs')
|
||||
|
||||
const gm = require('gm')
|
||||
|
||||
const tesseract = require("node-tesseract-ocr")
|
||||
const filePath = '../staticFiles/'
|
||||
|
||||
// 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 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) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
// Attachment.downloadFileFromUrl('https://i.imgur.com/5PVufWa.jpg')
|
||||
|
||||
let params = [userId]
|
||||
let query = 'SELECT * FROM attachment WHERE user_id = ? '
|
||||
let query = 'SELECT * FROM attachment WHERE user_id = ? AND visible = 1 '
|
||||
|
||||
if(noteId && noteId > 0){
|
||||
query += 'AND note_id = ? '
|
||||
@@ -35,7 +69,7 @@ Attachment.search = (userId, noteId) => {
|
||||
Attachment.forNote = (userId, noteId) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.promise()
|
||||
.query(`SELECT * FROM attachment WHERE user_id = ? AND note_id = ? ORDER BY last_indexed DESC;`, [userId, noteId])
|
||||
.query(`SELECT * FROM attachment WHERE user_id = ? AND note_id = ? AND visible = 1 ORDER BY last_indexed DESC;`, [userId, noteId])
|
||||
.then((rows, fields) => {
|
||||
resolve(rows[0]) //Return all attachments found by query
|
||||
})
|
||||
@@ -67,42 +101,151 @@ Attachment.update = (userId, attachmentId, updatedText, noteId) => {
|
||||
})
|
||||
}
|
||||
|
||||
Attachment.delete = (attachmentId) => {
|
||||
console.log('Delete Attachment', attachmentId)
|
||||
Attachment.delete = (userId, attachmentId, urlDelete = false) => {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
db.promise()
|
||||
.query(`DELETE FROM attachment WHERE id = ?`, [attachmentId])
|
||||
.query('SELECT * FROM attachment WHERE id = ? AND user_id = ? LIMIT 1', [attachmentId, userId])
|
||||
.then((rows, fields) => {
|
||||
resolve(rows[0]) //Return all tags found by query
|
||||
|
||||
//Attachment doesn't exist, return done
|
||||
if(rows[0].length == 0){
|
||||
return resolve(true)
|
||||
}
|
||||
|
||||
//Pull data we want out of
|
||||
let row = rows[0][0]
|
||||
let url = row.url
|
||||
const noteId = row.note_id
|
||||
|
||||
//Do not delete link attachments, just hide them. They will be deleted if removed from note
|
||||
if(row.attachment_type == 1 && !urlDelete){
|
||||
db.promise()
|
||||
.query(`UPDATE attachment SET visible = 0 WHERE id = ?`, [attachmentId])
|
||||
.then((rows, fields) => { })
|
||||
.catch(console.log)
|
||||
|
||||
return resolve(true)
|
||||
}
|
||||
|
||||
//Try to delete file and thumbnail
|
||||
try {
|
||||
fs.unlinkSync(filePath+row.file_location)
|
||||
} catch(err) { console.error('File Does not exist') }
|
||||
try {
|
||||
fs.unlinkSync(filePath+'thumb_'+row.file_location)
|
||||
} catch(err) { console.error('Thumbnail Does not exist') }
|
||||
|
||||
db.promise()
|
||||
.query(`DELETE FROM attachment WHERE id = ?`, [attachmentId])
|
||||
.then((rows, fields) => { })
|
||||
.catch(console.log)
|
||||
|
||||
//Normal notes are done, for links, remove the link from the note
|
||||
// if(row.attachment_type != 1){
|
||||
return resolve(true)
|
||||
// }
|
||||
|
||||
//The code below will delete URL text for note
|
||||
db.promise().query('SELECT text FROM note WHERE id = ? AND user_id = ?', [noteId, userId])
|
||||
.then((rows, fields) => {
|
||||
|
||||
//Grab note text
|
||||
let text = rows[0][0]['text']
|
||||
//Remove URL from text, then remove A tags with empty hrefs
|
||||
let newText = text.replace(url, '').replace(/<a href="">[^<]*<\/a>/g, '')
|
||||
|
||||
db.promise().query('UPDATE note SET text = ? WHERE id = ? AND user_id = ? LIMIT 1 ', [newText, noteId, userId])
|
||||
.then((rows, fields) => {
|
||||
|
||||
//Only reindex if note was deleted from menu
|
||||
if(reindex){
|
||||
const Note = require('@models/Note')
|
||||
//Reindex Note after updating text
|
||||
Note.reindex(userId, noteId)
|
||||
}
|
||||
|
||||
|
||||
return resolve(true)
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
})
|
||||
.catch(console.log)
|
||||
})
|
||||
}
|
||||
|
||||
Attachment.processUploadedFile = (userId, noteId, fileObject) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
const created = Math.round((+new Date)/1000)
|
||||
const fileLocation = fileObject.filename
|
||||
const fileName = fileObject.originalname
|
||||
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
|
||||
|
||||
// console.log('Adding file')
|
||||
// console.log( [noteId, userId, 2, fileName, created, fileLocation] )
|
||||
//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)
|
||||
|
||||
//Create attachment in DB with scrape text and provided data
|
||||
|
||||
db.promise()
|
||||
.query(`
|
||||
INSERT INTO attachment
|
||||
(note_id, user_id, attachment_type, \`text\`, last_indexed, file_location)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?, ?)
|
||||
`, [noteId, userId, 2, fileName, created, fileLocation])
|
||||
.then((rows, fields) => {
|
||||
console.log('Created attachment for ',fileName)
|
||||
resolve({ fileName, fileLocation }) //Return found text
|
||||
})
|
||||
.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)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -121,10 +264,30 @@ Attachment.scanTextForWebsites = (userId, noteId, noteText) => {
|
||||
const urlPattern = /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])/igm
|
||||
let allUrls = noteText.match(urlPattern)
|
||||
|
||||
//Remove all duplicates
|
||||
let foundUrls = [...new Set(allUrls)]
|
||||
if(allUrls == null){
|
||||
allUrls = []
|
||||
}
|
||||
|
||||
//Go through each attachment, check for existing URLs
|
||||
//Every URL needs HTTPS
|
||||
let foundUrls = []
|
||||
allUrls.forEach( (item, index) => {
|
||||
//Every URL should have HTTPS
|
||||
if(item.indexOf('https://') == -1 && item.indexOf('http://') == -1){
|
||||
allUrls[index] = 'https://'+item
|
||||
}
|
||||
//URLs should all have HTTPS!!!
|
||||
if(item.indexOf('http://') >= 0){
|
||||
allUrls[index] = item.replace('http://','https://')
|
||||
}
|
||||
})
|
||||
|
||||
//Remove all duplicates
|
||||
foundUrls = [...new Set(allUrls)]
|
||||
|
||||
|
||||
|
||||
//Go through each saved URL, remove new URLs from saved URLs
|
||||
//If a URL is not found, delete it
|
||||
attachments.forEach(attachment => {
|
||||
//URL already scraped, push text and continue
|
||||
let urlIndex = foundUrls.indexOf( attachment.url )
|
||||
@@ -134,7 +297,7 @@ Attachment.scanTextForWebsites = (userId, noteId, noteText) => {
|
||||
foundUrls.splice(urlIndex, 1) //Remove existing from set of found
|
||||
} else {
|
||||
//If existing attachment is not found in note, remove it
|
||||
Attachment.delete(attachment.id)
|
||||
Attachment.delete(userId, attachment.id, true)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -183,18 +346,22 @@ Attachment.scrapeUrlsCreateAttachments = (userId, noteId, foundUrls) => {
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Attachment.downloadFileFromUrl = (url) => {
|
||||
|
||||
//File Path
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
if(url == null){
|
||||
resolve(null)
|
||||
}
|
||||
|
||||
const random = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
|
||||
const filePath = '../staticFiles/'
|
||||
let fileName = filePath + random + '_img'
|
||||
const extension = '.'+url.split('.').pop() //This is throwing an error
|
||||
let fileName = random+'_scrape'+extension
|
||||
const thumbPath = 'thumb_'+fileName
|
||||
|
||||
console.log('Scraping image url')
|
||||
console.log(url)
|
||||
|
||||
console.log('Getting ready to scrape ', url)
|
||||
|
||||
@@ -207,15 +374,24 @@ Attachment.downloadFileFromUrl = (url) => {
|
||||
console.log(res.statusCode)
|
||||
console.log(res.headers['content-type'])
|
||||
})
|
||||
.pipe(fs.createWriteStream(fileName))
|
||||
.pipe(fs.createWriteStream(filePath+thumbPath))
|
||||
.on('close', () => {
|
||||
|
||||
//resize image if its real big
|
||||
gm(filePath+thumbPath)
|
||||
.resize(550) //Resize to width of 550 px
|
||||
.quality(75) //compression level 0 - 100 (best)
|
||||
.write(filePath+thumbPath, function (err) {
|
||||
if(err){ console.log(err) }
|
||||
})
|
||||
|
||||
|
||||
console.log('Saved Image')
|
||||
resolve(random + '_img')
|
||||
resolve(fileName)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Attachment.processUrl = (userId, noteId, url) => {
|
||||
|
||||
const scrapeTime = 20*1000;
|
||||
@@ -231,8 +407,6 @@ Attachment.processUrl = (userId, noteId, url) => {
|
||||
|
||||
var removeWhitespace = /\s+/g
|
||||
|
||||
|
||||
// console.log('Scraping ', website)
|
||||
const options = {
|
||||
uri: url,
|
||||
simple: true,
|
||||
@@ -247,8 +421,23 @@ Attachment.processUrl = (userId, noteId, url) => {
|
||||
|
||||
let requestTimeout = null
|
||||
let thumbnail = null
|
||||
let request = null
|
||||
let created = Math.round((+new Date)/1000)
|
||||
let insertedId = null
|
||||
|
||||
let request = rp(options)
|
||||
//Create a shell attachment for each URL, put in processing state
|
||||
db.promise()
|
||||
.query(`INSERT INTO attachment
|
||||
(note_id, user_id, attachment_type, text, url, last_indexed, file_location)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
[noteId, userId, 1, 'Processing...', url, created, null])
|
||||
.then((rows, fields) => {
|
||||
//Set two bigger variables then return request for processing
|
||||
request = rp(options)
|
||||
insertedId = rows[0].insertId
|
||||
|
||||
return request
|
||||
})
|
||||
.then($ => {
|
||||
|
||||
clearTimeout(requestTimeout)
|
||||
@@ -258,16 +447,12 @@ Attachment.processUrl = (userId, noteId, url) => {
|
||||
let pageTitle = $('title').text().replace(removeWhitespace, " ")
|
||||
desiredSearchText += pageTitle + "\n"
|
||||
|
||||
let header = $('h1').text().replace(removeWhitespace, " ")
|
||||
desiredSearchText += header + "\n"
|
||||
// let header = $('h1').text().replace(removeWhitespace, " ")
|
||||
// desiredSearchText += header + "\n"
|
||||
|
||||
//Scrape metadata for page image
|
||||
let metadata = $('meta[property="og:image"]')
|
||||
//'meta[property="og:image"]' .conten()
|
||||
console.log('Scrape metadata')
|
||||
// console.log(metadata)
|
||||
if(metadata && metadata[0] && metadata[0].attribs){
|
||||
console.log('Found metadata image')
|
||||
console.log(metadata[0].attribs.content)
|
||||
thumbnail = metadata[0].attribs.content
|
||||
}
|
||||
|
||||
@@ -310,32 +495,51 @@ Attachment.processUrl = (userId, noteId, url) => {
|
||||
});
|
||||
|
||||
let finalWords = []
|
||||
for(let i=0; i<15; i++){
|
||||
for(let i=0; i<5; i++){
|
||||
if(sortable[i] && sortable[i][0]){
|
||||
finalWords.push(sortable[i][0])
|
||||
}
|
||||
}
|
||||
|
||||
desiredSearchText += finalWords.join(', ')
|
||||
console.log('TexT Scraped')
|
||||
console.log(desiredSearchText)
|
||||
if(finalWords.length > 0){
|
||||
desiredSearchText += 'Keywords: ' + finalWords.join(', ')
|
||||
}
|
||||
|
||||
const created = Math.round((+new Date)/1000)
|
||||
|
||||
// console.log('TexT Scraped')
|
||||
// console.log(desiredSearchText)
|
||||
|
||||
created = Math.round((+new Date)/1000)
|
||||
|
||||
//Scrape URL for thumbnail - take filename and save in attachment
|
||||
Attachment.downloadFileFromUrl(thumbnail)
|
||||
.then(thumbnailFilename => {
|
||||
|
||||
//Create attachment in DB with scrape text and provided data
|
||||
//Update text and thumbnail filename
|
||||
created = Math.round((+new Date)/1000)
|
||||
db.promise()
|
||||
.query(`INSERT INTO attachment
|
||||
(note_id, user_id, attachment_type, text, url, last_indexed, file_location)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`, [noteId, userId, 1, desiredSearchText, url, created, thumbnailFilename])
|
||||
.query(`UPDATE attachment SET
|
||||
text = ?,
|
||||
last_indexed = ?,
|
||||
file_location = ?
|
||||
WHERE id = ?
|
||||
`, [desiredSearchText, created, thumbnailFilename, insertedId])
|
||||
.then((rows, fields) => {
|
||||
|
||||
resolve(desiredSearchText) //Return found text
|
||||
})
|
||||
.catch(console.log)
|
||||
|
||||
|
||||
//Create attachment in DB with scrape text and provided data
|
||||
// db.promise()
|
||||
// .query(`INSERT INTO attachment
|
||||
// (note_id, user_id, attachment_type, text, url, last_indexed, file_location)
|
||||
// VALUES (?, ?, ?, ?, ?, ?, ?)`, [noteId, userId, 1, desiredSearchText, url, created, thumbnailFilename])
|
||||
// .then((rows, fields) => {
|
||||
|
||||
// resolve(desiredSearchText) //Return found text
|
||||
// })
|
||||
// .catch(console.log)
|
||||
})
|
||||
|
||||
})
|
||||
@@ -349,19 +553,30 @@ Attachment.processUrl = (userId, noteId, url) => {
|
||||
console.log('Cancel the request, its taking to long.')
|
||||
request.cancel()
|
||||
|
||||
desiredSearchText = url
|
||||
const created = Math.round((+new Date)/1000)
|
||||
desiredSearchText = 'No Description for -> '+url
|
||||
|
||||
//Create attachment in DB with scrape text and provided data
|
||||
created = Math.round((+new Date)/1000)
|
||||
db.promise()
|
||||
.query(`INSERT INTO attachment
|
||||
(note_id, user_id, attachment_type, text, url, last_indexed)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`, [noteId, userId, 1, desiredSearchText, url, created])
|
||||
.query(`UPDATE attachment SET
|
||||
text = ?,
|
||||
last_indexed = ?,
|
||||
WHERE id = ?
|
||||
`, [desiredSearchText, created, insertedId])
|
||||
.then((rows, fields) => {
|
||||
resolve(desiredSearchText) //Return found text
|
||||
})
|
||||
.catch(console.log)
|
||||
|
||||
}, (scrapeTime))
|
||||
//Create attachment in DB with scrape text and provided data
|
||||
// db.promise()
|
||||
// .query(`INSERT INTO attachment
|
||||
// (note_id, user_id, attachment_type, text, url, last_indexed)
|
||||
// VALUES (?, ?, ?, ?, ?, ?)`, [noteId, userId, 1, desiredSearchText, url, created])
|
||||
// .then((rows, fields) => {
|
||||
// resolve(desiredSearchText) //Return found text
|
||||
// })
|
||||
// .catch(console.log)
|
||||
|
||||
}, scrapeTime )
|
||||
})
|
||||
}
|
@@ -12,6 +12,37 @@ const fs = require('fs')
|
||||
|
||||
let Note = module.exports = {}
|
||||
|
||||
const gm = require('gm')
|
||||
|
||||
Note.fixAttachmentThumbnails = () => {
|
||||
const filePath = '../staticFiles/'
|
||||
db.promise()
|
||||
.query(`SELECT * FROM attachment WHERE file_location NOT LIKE "%.%"`)
|
||||
.then( (rows, fields) => {
|
||||
|
||||
rows[0].forEach(line => {
|
||||
|
||||
const rawFilename = line['file_location']
|
||||
const goodFileName = rawFilename+'.jpg'
|
||||
|
||||
//Rename file to have jpg extension, create thumbnail, update database
|
||||
fs.rename(filePath+rawFilename, filePath+goodFileName, (err) => {
|
||||
|
||||
db.promise()
|
||||
.query(`UPDATE attachment SET file_location = ? WHERE id = ?`,[goodFileName, line['id'] ])
|
||||
.then( (rows, fields) => {
|
||||
gm(filePath+goodFileName)
|
||||
.resize(550) //Resize to width of 550 px
|
||||
.quality(75) //compression level 0 - 100 (best)
|
||||
.write(filePath + 'thumb_'+goodFileName, function (err) {
|
||||
console.log('Done for -> ', goodFileName)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Note.stressTest = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -81,33 +112,30 @@ Note.reindex = (userId, noteId) => {
|
||||
|
||||
Note.get(userId, noteId)
|
||||
.then(note => {
|
||||
|
||||
const noteText = note.text
|
||||
|
||||
//Process note text and attachment data
|
||||
Attachment.scanTextForWebsites(userId, noteId, noteText)
|
||||
.then( allNoteAttachmentText => {
|
||||
//
|
||||
// Update Solr index
|
||||
//
|
||||
Tags.string(userId, noteId)
|
||||
.then(tagString => {
|
||||
//
|
||||
// Update Solr index
|
||||
//
|
||||
Tags.string(userId, noteId)
|
||||
.then(tagString => {
|
||||
|
||||
const fullText = ProcessText.removeHtml(noteText) +' '+ tagString +' '+ ProcessText.removeHtml(allNoteAttachmentText)
|
||||
const fullText = ProcessText.removeHtml(noteText) +' '+ tagString
|
||||
|
||||
db.promise()
|
||||
.query(`
|
||||
|
||||
INSERT INTO note_text_index (note_id, user_id, text)
|
||||
VALUES (?,?,?)
|
||||
ON DUPLICATE KEY UPDATE text = ?
|
||||
db.promise()
|
||||
.query(`
|
||||
|
||||
INSERT INTO note_text_index (note_id, user_id, text)
|
||||
VALUES (?,?,?)
|
||||
ON DUPLICATE KEY UPDATE text = ?
|
||||
|
||||
`, [noteId, userId, fullText, fullText])
|
||||
.then((rows, fields) => {
|
||||
resolve(true)
|
||||
})
|
||||
.catch(console.log)
|
||||
|
||||
`, [noteId, userId, fullText, fullText])
|
||||
.then((rows, fields) => {
|
||||
resolve(true)
|
||||
})
|
||||
.catch(console.log)
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
@@ -132,6 +160,9 @@ Note.update = (userId, noteId, noteText, color, pinned, archived) => {
|
||||
|
||||
//Async solr note reindex
|
||||
Note.reindex(userId, noteId)
|
||||
|
||||
//Async attachment reindex
|
||||
Attachment.scanTextForWebsites(userId, noteId, noteText)
|
||||
|
||||
//Send back updated response
|
||||
resolve(rows[0])
|
||||
@@ -147,29 +178,15 @@ Note.update = (userId, noteId, noteText, color, pinned, archived) => {
|
||||
Note.delete = (userId, noteId) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
//
|
||||
// Delete, note, text index, tags
|
||||
// Leave the attachments, they can be deleted on their own
|
||||
//
|
||||
|
||||
db.promise().query('DELETE FROM note WHERE note.id = ? AND note.user_id = ?', [noteId,userId])
|
||||
.then((rows, fields) => {
|
||||
return db.promise().query('DELETE FROM note_text_index WHERE note_text_index.note_id = ? AND note_text_index.user_id = ?', [noteId,userId])
|
||||
})
|
||||
.then((rows, fields) => {
|
||||
//Select all attachments with files
|
||||
return db.promise().query('SELECT file_location FROM attachment WHERE attachment.note_id = ? AND attachment.user_id = ?', [noteId,userId])
|
||||
})
|
||||
.then((attachmentRows, fields) => {
|
||||
|
||||
//Go through each selected attachment and delete the files
|
||||
attachmentRows[0].forEach( location => {
|
||||
const fileName = location['file_location']
|
||||
if(fileName != null && fileName.length > 1){
|
||||
fs.unlink('../staticFiles/'+fileName ,function(err){ //Async, just rip through them.
|
||||
if(err) return console.log(err);
|
||||
// console.log('file deleted successfully => ', fileName);
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return db.promise().query('DELETE FROM attachment WHERE attachment.note_id = ? AND attachment.user_id = ?', [noteId,userId])
|
||||
})
|
||||
.then((rows, fields) => {
|
||||
return db.promise().query('DELETE FROM note_tag WHERE note_tag.note_id = ? AND note_tag.user_id = ?', [noteId,userId])
|
||||
})
|
||||
@@ -449,6 +466,7 @@ Note.search = (userId, searchQuery, searchTags, fastFilters) => {
|
||||
|
||||
//Deduce note title
|
||||
const textData = ProcessText.deduceNoteTitle(note.text)
|
||||
// console.log(textData)
|
||||
|
||||
// console.log(textData)
|
||||
|
||||
|
@@ -22,6 +22,11 @@ router.post('/search', function (req, res) {
|
||||
.then( data => res.send(data) )
|
||||
})
|
||||
|
||||
router.post('/textsearch', function (req, res) {
|
||||
Attachment.textSearch(userId, req.body.searchTerm)
|
||||
.then( data => res.send(data) )
|
||||
})
|
||||
|
||||
router.post('/get', function (req, res) {
|
||||
Attachment.forNote(userId, req.body.noteId)
|
||||
.then( data => res.send(data) )
|
||||
@@ -35,6 +40,11 @@ router.post('/update', function (req, res) {
|
||||
})
|
||||
})
|
||||
|
||||
router.post('/delete', function (req, res) {
|
||||
Attachment.delete(userId, req.body.attachmentId)
|
||||
.then( data => res.send(data) )
|
||||
})
|
||||
|
||||
router.post('/upload', upload.single('file'), function (req, res, next) {
|
||||
|
||||
//Create attachment with file information and node id
|
||||
|
@@ -2,20 +2,30 @@ var express = require('express')
|
||||
var router = express.Router()
|
||||
|
||||
let Notes = require('@models/Note');
|
||||
|
||||
let userId = null
|
||||
let socket = null
|
||||
|
||||
// middleware that is specific to this router
|
||||
router.use(function setUserId (req, res, next) {
|
||||
if(userId = req.headers.userId){
|
||||
if(req.headers.userId){
|
||||
userId = req.headers.userId
|
||||
}
|
||||
if(req.headers.socket){
|
||||
// socket = req.
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
router.post('/get', function (req, res) {
|
||||
req.io.emit('welcome_homie', 'Welcome, dont poop from excitement')
|
||||
Notes.get(userId, req.body.noteId)
|
||||
.then( data => res.send(data) )
|
||||
.then( data => {
|
||||
//Join room when user opens note
|
||||
// req.io.join('note_room')
|
||||
res.send(data)
|
||||
})
|
||||
})
|
||||
|
||||
router.post('/delete', function (req, res) {
|
||||
@@ -35,11 +45,12 @@ router.post('/update', function (req, res) {
|
||||
|
||||
router.post('/search', function (req, res) {
|
||||
Notes.search(userId, req.body.searchQuery, req.body.searchTags, req.body.fastFilters)
|
||||
.then( notesAndTags => res.send(notesAndTags))
|
||||
.then( notesAndTags => {
|
||||
res.send(notesAndTags)
|
||||
})
|
||||
})
|
||||
|
||||
router.post('/difftext', function (req, res) {
|
||||
|
||||
Notes.getDiffText(userId, req.body.noteId, req.body.text, req.body.updated)
|
||||
.then( fullDiffText => {
|
||||
//Response should be full diff text
|
||||
@@ -50,6 +61,9 @@ router.post('/difftext', function (req, res) {
|
||||
//Reindex all notes. Not a very good function, not public
|
||||
router.get('/reindex5yu43prchuj903mrc', function (req, res) {
|
||||
|
||||
Notes.fixAttachmentThumbnails()
|
||||
res.send('A whole mess is going on in the background')
|
||||
|
||||
// Notes.stressTest().then( i => {
|
||||
// // Notes.reindexAll().then( result => res.send('Welcome to reindex...oh god'))
|
||||
// })
|
||||
|
Reference in New Issue
Block a user