before changes
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
const AWS = require('aws-sdk')
|
||||
const fs = require('fs')
|
||||
const { stringifyError } = require('../utils/logService')
|
||||
var s3 = new AWS.S3()
|
||||
|
||||
const fetchS3Object = async ({ fileName, bucket, filePath }) => {
|
||||
filePath = filePath || '/tmp/' + fileName
|
||||
console.log('Fetching', stringifyObj({ fileName, filePath }))
|
||||
try {
|
||||
var params = { Bucket: bucket, Key: fileName }
|
||||
const downloadResult = await s3.getObject(params).promise()
|
||||
fs.writeFileSync(filePath, downloadResult.Body, function (err) {
|
||||
if (err) console.log(err.code, '-', err.message)
|
||||
})
|
||||
return filePath
|
||||
} catch (error) {
|
||||
console.log('Download from S3 Error:', stringifyError(error))
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const upload = ({
|
||||
filePath,
|
||||
fileName,
|
||||
bucket,
|
||||
contentType,
|
||||
fileType,
|
||||
// accessControl,
|
||||
}) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// accessControl = accessControl || 'public-read'
|
||||
fs.readFile(filePath, function (err, data) {
|
||||
if (err) reject(err)
|
||||
const params = {
|
||||
Bucket: bucket, // pass your bucket name
|
||||
Key: fileName,
|
||||
Body: data,
|
||||
// ContentType: contentType,
|
||||
// ContentDisposition: `inline; fileName=${fileName}.${fileType}`,
|
||||
// ACL: accessControl,
|
||||
}
|
||||
s3.upload(params, function (err, data) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
console.log(`${fileName} Upload to s3`, stringifyError(err))
|
||||
} else {
|
||||
console.log(
|
||||
`Successfully uploaded data ${fileName}`,
|
||||
stringifyError(data)
|
||||
)
|
||||
resolve(data.Location)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
upload,
|
||||
fetchS3Object,
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
const AWS = require('aws-sdk')
|
||||
AWS.config.update({ region: 'us-west-2' })
|
||||
const sqsService = require('./sqs_service')
|
||||
const sqs = new AWS.SQS({ apiVersion: '2012-11-05' })
|
||||
|
||||
module.exports = sqsService
|
||||
@@ -0,0 +1,83 @@
|
||||
const AWS = require('aws-sdk')
|
||||
|
||||
const sqs = new AWS.SQS({ apiVersion: '2012-11-05' })
|
||||
|
||||
const StringifyUtils = require('../utils/logService')
|
||||
|
||||
const fetchMessageFromSQS = (sqsQueueUrl, waitTimeInSeconds = 0) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const params = {
|
||||
WaitTimeSeconds: waitTimeInSeconds,
|
||||
QueueUrl: sqsQueueUrl /* required */,
|
||||
}
|
||||
sqs.receiveMessage(params, function (err, data) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
console.log(
|
||||
`ERROR in fetchJobFromSQS : `,
|
||||
StringifyUtils.stringifyError(err)
|
||||
)
|
||||
} else {
|
||||
resolve(data)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const deleteMessageFromSQS = (sqsQueueUrl, receiptHandle) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const params = {
|
||||
ReceiptHandle: receiptHandle,
|
||||
QueueUrl: sqsQueueUrl /* required */,
|
||||
}
|
||||
sqs.deleteMessage(params, function (err, data) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
console.log(
|
||||
`ERROR in sending delete request to AWS.SQS : `,
|
||||
StringifyUtils.stringifyError(err)
|
||||
)
|
||||
} else {
|
||||
console.log(
|
||||
'Successfully sent delete request to AWS.SQS',
|
||||
StringifyUtils.stringifyError(data)
|
||||
)
|
||||
resolve(data)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const sendMessageToSQS = (sqsQueueUrl, message) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const params = {
|
||||
MessageBody: message,
|
||||
QueueUrl: sqsQueueUrl /* required */,
|
||||
// MessageGroupId:
|
||||
// process.env.POTION_APP_ENV ||
|
||||
// '' + `_` + uuidV4() + '_' + new Date().toISOString(),
|
||||
// MessageDeduplicationId: uuidV4() + `_` + new Date().toISOString()
|
||||
}
|
||||
sqs.sendMessage(params, function (err, data) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
console.log(
|
||||
`ERROR in seding request to AWS.SQS : `,
|
||||
StringifyUtils.stringifyError(err)
|
||||
)
|
||||
} else {
|
||||
console.log(
|
||||
'Successfully sent request to AWS.SQS',
|
||||
StringifyUtils.stringifyError(data)
|
||||
)
|
||||
resolve(data.Location)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchMessageFromSQS,
|
||||
deleteMessageFromSQS,
|
||||
sendMessageToSQS,
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
const Bugsnag = require('@bugsnag/js')
|
||||
|
||||
const DEV_APP_ENVS = ['local-dev', 'development']
|
||||
|
||||
const handleError = (err, user) => {
|
||||
const isDevEnv = DEV_APP_ENVS.includes(process.env.POTION_APP_ENV)
|
||||
if (isDevEnv) {
|
||||
return
|
||||
}
|
||||
|
||||
if (user)
|
||||
Bugsnag.notify(err, function (event) {
|
||||
event.setUser(user._id, user.email, user.name)
|
||||
})
|
||||
else Bugsnag.notify(err)
|
||||
}
|
||||
|
||||
module.exports = handleError
|
||||
@@ -0,0 +1,9 @@
|
||||
const deleteFile = (filePath) => {
|
||||
return new Promise((resolve) => {
|
||||
require('fs').unlinkSync(filePath)
|
||||
console.log(`[deleted] ${filePath}`)
|
||||
resolve()
|
||||
})
|
||||
}
|
||||
|
||||
exports.deleteFile = deleteFile
|
||||
@@ -0,0 +1,19 @@
|
||||
const fs = require('fs')
|
||||
|
||||
const importedModules = {}
|
||||
const files = fs.readdirSync(__dirname)
|
||||
|
||||
for (const file of files) {
|
||||
const fileNameWithoutExtension = file.replace(/\.[^.]*$/, '')
|
||||
const fileExtension = file.split('.').pop()
|
||||
|
||||
if (
|
||||
fileNameWithoutExtension !== 'index' &&
|
||||
(fileExtension === 'js' || fileExtension === 'ts')
|
||||
)
|
||||
importedModules[fileNameWithoutExtension] = require(`./${file}`)[
|
||||
fileNameWithoutExtension
|
||||
]
|
||||
}
|
||||
|
||||
module.exports = importedModules
|
||||
@@ -0,0 +1,14 @@
|
||||
const stringifyError = (error = {}) => {
|
||||
return JSON.stringify(error, Object.getOwnPropertyNames(error))
|
||||
}
|
||||
|
||||
const potionErrorObj = (error = {}, details = {}) => {
|
||||
const stringifiedError = stringifyError(error)
|
||||
const stringifiedObject = JSON.stringify({
|
||||
error: stringifiedError,
|
||||
details,
|
||||
})
|
||||
return stringifiedObject
|
||||
}
|
||||
|
||||
module.exports = { potionErrorObj, stringifyError }
|
||||
@@ -0,0 +1,4 @@
|
||||
const VoiceCloning = require('./voice_cloning_model')
|
||||
const VoiceCloningService = require('./voice_cloning_service')
|
||||
|
||||
module.exports = VoiceCloningService(VoiceCloning)
|
||||
@@ -0,0 +1,44 @@
|
||||
const mongoose = require('mongoose')
|
||||
const Schema = mongoose.Schema
|
||||
|
||||
const VoiceCloningSchema = Schema(
|
||||
{
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'User',
|
||||
required: true,
|
||||
},
|
||||
userAudioProfileId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'UserAudioProfile',
|
||||
required: true,
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
required: false,
|
||||
default: 'created',
|
||||
},
|
||||
input: {
|
||||
type: Schema.Types.Mixed,
|
||||
default: null,
|
||||
},
|
||||
training_model: {
|
||||
type: Schema.Types.Mixed,
|
||||
default: null,
|
||||
},
|
||||
metadata: {
|
||||
type: Schema.Types.Mixed,
|
||||
default: null,
|
||||
},
|
||||
deleted: {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
default: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
)
|
||||
|
||||
module.exports = mongoose.model('VoiceCloning', VoiceCloningSchema)
|
||||
@@ -0,0 +1,140 @@
|
||||
const StringifyUtils = require('../utils/logService')
|
||||
|
||||
const create = (VoiceCloningModel) => async (data) => {
|
||||
try {
|
||||
const newModel = new VoiceCloningModel({ ...data })
|
||||
const savedModel = await newModel.save()
|
||||
return savedModel
|
||||
} catch (error) {
|
||||
const details = { data }
|
||||
console.log(
|
||||
'ERROR - VOICE CLONING SERVICE > create',
|
||||
StringifyUtils.potionErrorObj(error, details)
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const insertMany = (VoiceCloningModel) => async (data) => {
|
||||
try {
|
||||
const inserted = await VoiceCloningModel.insertMany(data)
|
||||
return inserted
|
||||
} catch (error) {
|
||||
const details = { data }
|
||||
console.log(
|
||||
'ERROR - VOICE CLONING SERVICE > insertMany',
|
||||
StringifyUtils.potionErrorObj(error, details)
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const read = (VoiceCloningModel) => async (filter) => {
|
||||
try {
|
||||
const foundModel = await VoiceCloningModel.findOne({
|
||||
...filter,
|
||||
deleted: false,
|
||||
})
|
||||
return foundModel
|
||||
} catch (error) {
|
||||
const details = { filter }
|
||||
console.log(
|
||||
'ERROR - VOICE CLONING SERVICE > read',
|
||||
StringifyUtils.potionErrorObj(error, details)
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const find = (VoiceCloningModel) => async (filter) => {
|
||||
try {
|
||||
const foundModels = await VoiceCloningModel.find({
|
||||
...filter,
|
||||
deleted: false,
|
||||
})
|
||||
return foundModels
|
||||
} catch (error) {
|
||||
const details = { filter }
|
||||
console.log(
|
||||
'ERROR - VOICE CLONING SERVICE > find',
|
||||
StringifyUtils.potionErrorObj(error, details)
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const update = (VoiceCloningModel) => async (data) => {
|
||||
try {
|
||||
const updatedModel = await VoiceCloningModel.findOneAndUpdate(
|
||||
{ _id: data._id },
|
||||
data,
|
||||
{
|
||||
new: true,
|
||||
}
|
||||
)
|
||||
return updatedModel
|
||||
} catch (error) {
|
||||
const details = { data }
|
||||
console.log(
|
||||
'ERROR - VOICE CLONING SERVICE > update',
|
||||
StringifyUtils.potionErrorObj(error, details)
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const remove = (VoiceCloningModel) => async (filter) => {
|
||||
try {
|
||||
const updatedModel = await VoiceCloningModel.findOneAndUpdate(
|
||||
{ ...filter },
|
||||
{
|
||||
$set: {
|
||||
deleted: true,
|
||||
},
|
||||
},
|
||||
{ new: true }
|
||||
)
|
||||
return updatedModel
|
||||
} catch (error) {
|
||||
const details = { filter }
|
||||
console.log(
|
||||
'ERROR - VOICE CLONING SERVICE > remove',
|
||||
StringifyUtils.potionErrorObj(error, details)
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const removeMany = (VoiceCloningModel) => async (filter) => {
|
||||
try {
|
||||
const updatedModel = await VoiceCloningModel.updateMany(
|
||||
{ ...filter },
|
||||
{
|
||||
$set: {
|
||||
deleted: true,
|
||||
},
|
||||
},
|
||||
{ new: true }
|
||||
)
|
||||
return updatedModel
|
||||
} catch (error) {
|
||||
const details = { filter }
|
||||
console.log(
|
||||
'ERROR - VOICE CLONING SERVICE > removeMany',
|
||||
StringifyUtils.potionErrorObj(error, details)
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (VoiceCloningModel) => {
|
||||
return {
|
||||
create: create(VoiceCloningModel),
|
||||
insertMany: insertMany(VoiceCloningModel),
|
||||
read: read(VoiceCloningModel),
|
||||
remove: remove(VoiceCloningModel),
|
||||
removeMany: removeMany(VoiceCloningModel),
|
||||
update: update(VoiceCloningModel),
|
||||
find: find(VoiceCloningModel),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user