-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
83 lines (75 loc) · 2.33 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const connection = require('./connection')
const fs = require('fs')
const AWS = require('aws-sdk')
const s3 = new AWS.S3({
accessKeyId: 'AKIASW2CKK37Z3F7ZOU5',
secretAccessKey: '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'
})
const uuid = require('uuid')
const colors = require('colors')
const uploadFile = (filename) => {
return new Promise((resolve, reject) => {
const fileContent = fs.readFileSync(filename);
const KeyFilename = uuid.v4()+'.PDF'
const params = {
Bucket: 'nodejs-upload-to-s3',
Key: KeyFilename,
Body: fileContent
}
s3.upload(params, function(err, data) {
if (err) {
reject(err)
}
console.log(`File uploaded successfully. ${data.Location}`);
resolve(data)
})
})
}
const registerSaveDb = (location) => {
return new Promise((resolve, reject) => {
console.log('Registar upload ...')
const data = {
filename: location
}
const sql = 'INSERT INTO upload_logs SET ?'
connection.query(sql, [data], (error, result) => {
if (error) {
console.log('Error al agregar registro')
console.log(error)
reject(error)
} else {
console.log(`Registro agregado correctamente: ${new Date()}`)
resolve(true)
}
})
})
}
const deleteFile = (filename) => {
return new Promise((resolve, reject) => {
console.log('Delete local file ...')
console.log('Realmente no se elimina el archivo para que el proceso continue ...'.red)
resolve()
})
}
const registerDeleteDb = (location) => {
return new Promise((resolve, reject) => {
console.log('Registrar Delete ...')
const sql = 'UPDATE upload_logs SET status = 2 WHERE filename=?'
connection.query(sql, location, (error, result) => {
if (error) {
console.log('Error al registrar delete')
console.log(error)
reject(error)
} else {
console.log(`Delete registrado correctamente: ${new Date()}`)
resolve(true)
}
})
})
}
module.exports = {
uploadFile,
registerSaveDb,
deleteFile,
registerDeleteDb
}