Skip to content

Commit

Permalink
images
Browse files Browse the repository at this point in the history
  • Loading branch information
trdecker committed Mar 12, 2024
1 parent f2f6ce4 commit 21d4105
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 10 deletions.
16 changes: 12 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import express from 'express'
import dotenv from 'dotenv'

import notesRouter from './src/routes/notesRouter.js'
import usersRouter from './src/routes/usersRouter.js'
import cors from 'cors'
import config from './src/config/config.js'
import express from 'express'
import dotenv from 'dotenv'
import cors from 'cors'

dotenv.config()
const port = config.port ?? 80 // Default to 80

const app = express()

const corsOptions = {
origin: [config.devUrl],
origin: [...config.devUrls],
methods: 'GET, PUT, POST, DELETE, HEAD, PATCH, OPTIONS',
credentials: true,
allowedHeaders: 'Content-Type, Authorization'
}

// const corsOptions2 = {
// origin: ['http://your-frontend-domain'],
// methods: 'GET, PUT, POST, DELETE, HEAD, PATCH, OPTIONS',
// credentials: true,
// allowedHeaders: 'Content-Type, Authorization',
// };

app.use(cors(corsOptions))

app.get('/', (req, res) => {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
"dependencies": {
"@azure/cosmos": "^4.0.0",
"@azure/identity": "^3.3.2",
"@azure/storage-blob": "^12.17.0",
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2"
"jsonwebtoken": "^9.0.2",
"jwks-rsa": "^3.1.0"
}
}
17 changes: 16 additions & 1 deletion src/config/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { JwksClient } from 'jwks-rsa'
import dotenv from 'dotenv'

dotenv.config()

// Create our client with the needed options
const client = new JwksClient({
jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`,
strictSsl: true,
json: true
})

export default {
endpoint: process.env.COSMOS_ENDPOINT,
port: process.env.PORT,
key: process.env.PRIMARY_KEY,
databaseName: process.env.DATABASE_NAME,
notesContainerName: process.env.NOTES_CONTAINER_NAME,
usersContainerName: process.env.USERS_CONTAINER_NAME,
devUrl: process.env.DEV_URL
devUrls: [
process.env.DEV_URL,
process.env.DEV_URL2,
process.env.DEV_URL3,
process.env.DEV_URL4
],
client
}
7 changes: 4 additions & 3 deletions src/controllers/noteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const notesController = {
*/
async getNotes(req, res) {
try {
console.log('in get notes')
const { userId } = req.user
const requestedUserId = req.query.userId ?? null

Expand Down Expand Up @@ -93,8 +94,8 @@ const notesController = {
},

/**
* @param {*} req
* @param {*} res
* @param {Object} req
* @param {Object} res
* @returns
*/
async editNote(req, res) {
Expand Down Expand Up @@ -165,7 +166,7 @@ const notesController = {
async deleteNote(req, res) {
try {
const { userId } = req.user
const noteId = req.query.noteId
const noteId = req.query.noteId

const oldNote = await noteModel.getNote(noteId)
if (!oldNote) {
Expand Down
54 changes: 53 additions & 1 deletion src/models/noteModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,50 @@
* @author Tad Decker
*
* 11/11/2023
*
* 2/29/2024
* - Image and audio recording functonality
*/


import { generateId } from '../utils/commonUtils.js'
import { CosmosClient } from '@azure/cosmos'
import config from '../config/config.js'
import { BlobServiceClient } from '@azure/storage-blob'

const cosmosClient = new CosmosClient({
endpoint: config.endpoint,
key: config.key
})

// Connct to cosmosDB database
const database = cosmosClient.database(config.databaseName)
const container = database.container(config.notesContainerName)

// Paramters for blob storage
const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
const containerName = 'notemaster-images'
const containerClient = blobServiceClient.getContainerClient(containerName)

// List all blobs in the container for debugging
async function listBlobs() {
console.log('Blobs in the container:')
for await (const blob of containerClient.listBlobsFlat()) {
console.log(`- ${blob.name}`)
}
}

const uploadBlob = async (blobName, fileStream) => {
const blockBlobClient = containerClient.getBlockBlobClient(blobName)
await blockBlobClient.uploadAsync(fileStream)
console.log(`Blob ${blobName} uploaded successfully!`)
}

const deleteBlob = () => {

}

/**
* @function getNote get a single note, given an id
* @function getUserNotes get a list of notes belonging to a user
Expand Down Expand Up @@ -68,7 +97,9 @@ export default {
},

/**
* @description Create a new note belonging to a user
* @description Create a new note belonging to a user.
* Images are saved to a Microsoft Azure blob container titled "notemaster-images"
* Audio recordings are saved to a Microsoft Azure blob container titled "notemaster-audio"
* @param {String} userId
* @param {Object} newNote
* @returns createdNote
Expand All @@ -77,6 +108,15 @@ export default {
const id = generateId()
const note = { userId, id, ...newNote }
const { resource: createdNote } = await container.items.create(note)

// Save images
for (const image of newNote?.images) {
console.log(image?.fileName && image?.data)

uploadBlob(image.fileName, image.data)


}
return createdNote
},

Expand All @@ -90,6 +130,15 @@ export default {
async editNote(userId, noteId, newNote) {
const note = {userId, id: noteId, ...newNote}
const { resource: createdNote } = await container.item(noteId).replace(note)

// Save images
for (const image of newNote?.images) {
console.log(image?.fileName && image?.data)

// uploadBlob(image.fileName, image.data)


}
return createdNote
},

Expand All @@ -103,3 +152,6 @@ export default {
return statusCode
}
}

// userid: 17072365278015425
// noteId: 1708555176506909

0 comments on commit 21d4105

Please sign in to comment.