Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated dependencies #231

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion set_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export POSTGRES_USERNAME=postgres
export POSTGRES_PASSWORD=mypassword
export POSTGRES_HOST=postgres.cr9bldgsf1j6.us-east-1.rds.amazonaws.com
export POSTGRES_DB=postgres
export AWS_BUCKET=arn:aws:s3:::mybucket1200798
export AWS_BUCKET=mybucket1200798
export AWS_REGION=us-east-1
export AWS_PROFILE=default
export JWT_SECRET=testing
Expand Down
8,777 changes: 6,126 additions & 2,651 deletions udagram-api/package-lock.json

Large diffs are not rendered by default.

58 changes: 33 additions & 25 deletions udagram-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,43 @@
"author": "Gabriel Ruttner",
"license": "ISC",
"dependencies": {
"@types/bcrypt": "^3.0.0",
"@types/jsonwebtoken": "^8.3.2",
"aws-sdk": "^2.429.0",
"bcrypt": "^5.0.1",
"body-parser": "^1.18.3",
"@eslint/config-array": "^0.19.2",
"@eslint/object-schema": "^2.1.6",
"@types/bcrypt": "^5.0.2",
"@types/jsonwebtoken": "^9.0.5",
"@aws-sdk/client-s3": "^3.511.0",
"@aws-sdk/s3-request-presigner": "^3.511.0",
"@aws-sdk/credential-providers": "^3.511.0",
"bcrypt": "^5.1.1",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"email-validator": "^2.0.4",
"express": "^4.16.4",
"jsonwebtoken": "^8.5.1",
"pg": "^8.0.3",
"reflect-metadata": "^0.1.13",
"sequelize": "^5.21.4",
"sequelize-typescript": "^0.6.9"
"express": "^4.18.3",
"glob": "^11.0.1",
"jsonwebtoken": "^9.0.2",
"lru-cache": "^11.0.2",
"pg": "^8.11.3",
"reflect-metadata": "^0.2.1",
"rimraf": "^6.0.1",
"sequelize": "^6.37.1",
"sequelize-typescript": "^2.1.6",
"superagent": "^10.1.1"
},
"devDependencies": {
"@types/bluebird": "^3.5.26",
"@types/cors": "^2.8.6",
"@types/express": "^4.16.1",
"@types/node": "^11.11.6",
"@types/sequelize": "^4.27.44",
"@types/validator": "^10.9.0",
"@typescript-eslint/eslint-plugin": "^2.19.2",
"@typescript-eslint/parser": "^2.19.2",
"chai": "^4.2.0",
"chai-http": "^4.2.1",
"eslint": "^6.8.0",
"@types/bluebird": "^3.5.42",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/node": "^20.11.24",
"@types/sequelize": "^4.28.20",
"@types/validator": "^13.11.9",
"@typescript-eslint/eslint-plugin": "^7.1.1",
"@typescript-eslint/parser": "^7.1.1",
"chai": "^5.1.0",
"chai-http": "^4.4.0",
"eslint": "^8.57.0",
"eslint-config-google": "^0.14.0",
"mocha": "^6.1.4",
"ts-node-dev": "^1.0.0-pre.32",
"typescript": "^3.3.4000"
"mocha": "^10.3.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.3.3"
}
}
49 changes: 28 additions & 21 deletions udagram-api/src/aws.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import AWS = require('aws-sdk');
import {config} from './config/config';

import { S3Client } from "@aws-sdk/client-s3";
import { fromIni } from "@aws-sdk/credential-providers";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3";
import { config } from './config/config';

// Configure AWS
const credentials = new AWS.SharedIniFileCredentials({profile: config.aws_profile});
AWS.config.credentials = credentials;
const credentials = fromIni({ profile: config.aws_profile });

export const s3 = new AWS.S3({
signatureVersion: 'v4',
// Create S3 client
export const s3Client = new S3Client({
region: config.aws_region,
params: {Bucket: config.aws_media_bucket},
credentials: credentials,
});

// Generates an AWS signed URL for retrieving objects
export function getGetSignedUrl( key: string ): string {
const signedUrlExpireSeconds = 60 * 5;

return s3.getSignedUrl('getObject', {
export async function getGetSignedUrl(key: string): Promise<string> {
const command = new GetObjectCommand({
Bucket: config.aws_media_bucket,
Key: key,
Expires: signedUrlExpireSeconds,
Key: key
});

const signedUrl = await getSignedUrl(s3Client, command, {
expiresIn: 60 * 5 // 5 minutes
});

return signedUrl;
}

// Generates an AWS signed URL for uploading objects
export function getPutSignedUrl( key: string ): string {
const signedUrlExpireSeconds = 60 * 5;

return s3.getSignedUrl('putObject', {
export async function getPutSignedUrl(key: string): Promise<string> {
const command = new PutObjectCommand({
Bucket: config.aws_media_bucket,
Key: key,
Expires: signedUrlExpireSeconds,
Key: key
});
}

const signedUrl = await getSignedUrl(s3Client, command, {
expiresIn: 60 * 5 // 5 minutes
});

return signedUrl;
}
27 changes: 14 additions & 13 deletions udagram-api/src/controllers/v0/feed/routes/feed.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as c from '../../../../config/config';
const router: Router = Router();

export function requireAuth(req: Request, res: Response, next: NextFunction) {

if (!req.headers || !req.headers.authorization) {
return res.status(401).send({message: 'No authorization headers.'});
}
Expand All @@ -29,12 +30,14 @@ export function requireAuth(req: Request, res: Response, next: NextFunction) {
// Get all feed items
router.get('/', async (req: Request, res: Response) => {
const items = await FeedItem.findAndCountAll({order: [['id', 'DESC']]});
items.rows.map((item) => {
// Map items to include signed URLs
const itemsWithUrls = await Promise.all(items.rows.map(async (item) => {
if (item.url) {
item.url = AWS.getGetSignedUrl(item.url);
item.url = await AWS.getGetSignedUrl(item.url);
}
});
res.send(items);
return item;
}));
res.send({count: items.count, rows: itemsWithUrls});
});

// Get a feed resource
Expand All @@ -47,20 +50,19 @@ router.get('/:id',

// Get a signed url to put a new item in the bucket
router.get('/signed-url/:fileName',
requireAuth,
async (req: Request, res: Response) => {
const {fileName} = req.params;
const url = AWS.getPutSignedUrl(fileName);
res.status(201).send({url: url});
});
requireAuth,
async (req: Request, res: Response) => {
const {fileName} = req.params;
const url = await AWS.getPutSignedUrl(fileName);
res.status(201).send({url: url});
});

// Create feed with metadata
router.post('/',
requireAuth,
async (req: Request, res: Response) => {
const caption = req.body.caption;
const fileName = req.body.url; // same as S3 key name

if (!caption) {
return res.status(400).send({message: 'Caption is required or malformed.'});
}
Expand All @@ -75,8 +77,7 @@ router.post('/',
});

const savedItem = await item.save();

savedItem.url = AWS.getGetSignedUrl(savedItem.url);
savedItem.url = await AWS.getGetSignedUrl(savedItem.url);
res.status(201).send(savedItem);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ router.post('/login', async (req: Request, res: Response) => {
}

const jwt = generateJWT(user);

res.status(200).send({auth: true, token: jwt, user: user.short()});
});

Expand Down
6 changes: 6 additions & 0 deletions udagram-api/src/sequelize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ export const sequelize = new Sequelize({
'host': config.host,

'dialect': config.dialect,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
},
'storage': ':memory:',
});
Loading