I need to get back into using git. The hell is wrong with me!?

This commit is contained in:
Max G committed 2019-12-20 05:50:50 +00:00
1 parent 7b77bd37f3
commit 6fe39406b7
135 files changed
+53273 -699

No files matched your search

+154 -18
View File
@@ -2,21 +2,73 @@ let db = require('@config/database')
let Attachment = module.exports = {}
const cheerio = require('cheerio');
const rp = require('request-promise');
const cheerio = require('cheerio')
const rp = require('request-promise')
const request = require('request')
const fs = require('fs')
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 = ? '
if(noteId && noteId > 0){
query += 'AND note_id = ? '
params.push(noteId)
}
query += 'ORDER BY last_indexed DESC '
db.promise()
.query(query, params)
.then((rows, fields) => {
resolve(rows[0]) //Return all attachments found by query
})
.catch(console.log)
})
}
//Returns all attachments
Attachment.forNote = (userId, noteId) => {
return new Promise((resolve, reject) => {
db.promise()
.query(`SELECT * FROM attachment WHERE user_id = ? AND note_id = ? AND attachment_type = 1;`, [userId, noteId])
.query(`SELECT * FROM attachment WHERE user_id = ? AND note_id = ? ORDER BY last_indexed DESC;`, [userId, noteId])
.then((rows, fields) => {
resolve(rows[0]) //Return all tags found by query
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 = (attachmentId) => {
console.log('Delete Attachment', attachmentId)
return new Promise((resolve, reject) => {
db.promise()
.query(`DELETE FROM attachment WHERE id = ?`, [attachmentId])
@@ -27,6 +79,34 @@ Attachment.delete = (attachmentId) => {
})
}
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
// console.log('Adding file')
// console.log( [noteId, userId, 2, fileName, created, fileLocation] )
//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)
})
}
//Scans text for websites, returns all attachments
Attachment.scanTextForWebsites = (userId, noteId, noteText) => {
return new Promise((resolve, reject) => {
@@ -34,7 +114,7 @@ Attachment.scanTextForWebsites = (userId, noteId, noteText) => {
if(noteText.length == 0){ resolve(solrAttachmentText) }
Attachment.forNote(userId, noteId).then(attachments => {
Attachment.urlForNote(userId, noteId).then(attachments => {
//Find all URLs in text
const urlPattern = /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])/igm
@@ -52,6 +132,7 @@ Attachment.scanTextForWebsites = (userId, noteId, noteText) => {
solrAttachmentText += attachment.text
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)
}
})
@@ -75,11 +156,13 @@ Attachment.scanTextForWebsites = (userId, noteId, noteText) => {
Attachment.scrapeUrlsCreateAttachments = (userId, noteId, foundUrls) => {
return new Promise((resolve, reject) => {
if(foundUrls == null || foundUrls.length == 0){
return resolve('')
}
console.log('About to scrape')
console.log(foundUrls)
if(foundUrls == null || foundUrls.length == 0){resolve('')}
let processedCount = 0
let scrapedText = ''
@@ -100,7 +183,42 @@ Attachment.scrapeUrlsCreateAttachments = (userId, noteId, foundUrls) => {
}
Attachment.downloadFileFromUrl = (url) => {
//File Path
return new Promise((resolve, reject) => {
const random = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
const filePath = '../staticFiles/'
let fileName = filePath + random + '_img'
console.log('Getting ready to scrape ', url)
request(url)
.on('error', error => {
console.log(error)
resolve(null)
})
.on('response', res => {
console.log(res.statusCode)
console.log(res.headers['content-type'])
})
.pipe(fs.createWriteStream(fileName))
.on('close', () => {
console.log('Saved Image')
resolve(random + '_img')
})
})
}
Attachment.processUrl = (userId, noteId, url) => {
const scrapeTime = 20*1000;
return new Promise((resolve, reject) => {
const excludeWords = ['share','facebook','twitter','reddit','be','have','do','say','get','make','go','know','take','see','come','think','look','want',
@@ -117,7 +235,7 @@ Attachment.processUrl = (userId, noteId, url) => {
const options = {
uri: url,
simple: true,
timeout: 1000 * 10, // 10 seconds
timeout: scrapeTime,
headers: {
'User-Agent':'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' //Simulate google headers
},
@@ -127,6 +245,7 @@ Attachment.processUrl = (userId, noteId, url) => {
}
let requestTimeout = null
let thumbnail = null
let request = rp(options)
.then($ => {
@@ -141,6 +260,17 @@ Attachment.processUrl = (userId, noteId, url) => {
let header = $('h1').text().replace(removeWhitespace, " ")
desiredSearchText += header + "\n"
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
}
let majorContent = ''
majorContent += $('[class*=content]').text()
.replace(removeWhitespace, " ") //Remove all whitespace
@@ -191,15 +321,21 @@ Attachment.processUrl = (userId, noteId, url) => {
const created = Math.round((+new Date)/1000)
//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
//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
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)
})
.catch(console.log)
})
.catch(error => {
@@ -212,7 +348,7 @@ Attachment.processUrl = (userId, noteId, url) => {
console.log('Cancel the request, its taking to long.')
request.cancel()
desiredSearchText = 'Unable to Scrape URL at this time'
desiredSearchText = url
const created = Math.round((+new Date)/1000)
//Create attachment in DB with scrape text and provided data
@@ -225,6 +361,6 @@ Attachment.processUrl = (userId, noteId, url) => {
})
.catch(console.log)
}, (5000))
}, (scrapeTime))
})
}