Skip to content

Commit 279eb6a

Browse files
committed
boilerplate code with s3 bucket added
0 parents  commit 279eb6a

24 files changed

+4423
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
/.env
3+
/node_modules

api/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const express = require("express");
2+
const dotenv = require("dotenv");
3+
const corsConfig = require("../utils/cors-config");
4+
const videoContentRoutes = require("../routes/video-content");
5+
const fileServerRoutes = require("../routes/file-server");
6+
dotenv.config();
7+
require("../db/database");
8+
9+
const app = express();
10+
11+
const port = process.env.PORT || 3000;
12+
13+
app.use(corsConfig);
14+
app.use(express.json());
15+
16+
app.use("/video-content", videoContentRoutes);
17+
app.use("/file", fileServerRoutes);
18+
19+
app.get("/", (req, res) => {
20+
res.send("Hello World");
21+
});
22+
23+
app.listen(port, () => {
24+
console.log(`Server is running on port ${port}`);
25+
});
26+
27+
module.exports = app;

app.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const express = require("express");
2+
const dotenv = require("dotenv");
3+
const multer = require("multer");
4+
5+
const {
6+
S3Client,
7+
PutObjectCommand,
8+
GetObjectCommand,
9+
DeleteObjectCommand,
10+
} = require("@aws-sdk/client-s3");
11+
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
12+
13+
dotenv.config(); // Load environment variables from a .env file into process.env
14+
15+
const bucketName = process.env.AWS_BUCKET_NAME;
16+
const bucketRegion = process.env.AWS_REGION;
17+
const accessKey = process.env.ACCESS_KEY;
18+
const secretAccessKey = process.env.SECRET_ACCESS_KEY;
19+
20+
const s3 = new S3Client({
21+
region: bucketRegion,
22+
credentials: {
23+
accessKeyId: accessKey,
24+
secretAccessKey: secretAccessKey,
25+
},
26+
});
27+
28+
const storage = multer.memoryStorage();
29+
30+
const upload = multer({ storage: storage });
31+
32+
const app = express();
33+
const port = process.env.PORT || 3000;
34+
35+
app.get("/", (req, res) => {
36+
res.send("Hello, world!");
37+
});
38+
39+
app.post("/upload", upload.single("image"), async (req, res) => {
40+
try {
41+
console.log(req.body, "end of req body ");
42+
console.log(req.file);
43+
44+
const params = {
45+
Bucket: bucketName,
46+
Key: req.file.originalname,
47+
Body: req.file.buffer,
48+
ContentType: req.file.mimetype,
49+
};
50+
51+
const command = new PutObjectCommand(params);
52+
53+
await s3.send(command);
54+
55+
res.send("File uploaded successfully!");
56+
} catch (error) {
57+
console.log(error);
58+
res.status(500).send("Error uploading file");
59+
}
60+
});
61+
62+
app.get("/image/:imageName", async (req, res) => {
63+
try {
64+
const imageName = req.params.imageName;
65+
const params = {
66+
Bucket: bucketName,
67+
Key: imageName,
68+
};
69+
70+
const command = new GetObjectCommand(params);
71+
72+
const url = await getSignedUrl(s3, command, { expiresIn: 3600 });
73+
74+
res.send(url);
75+
} catch (error) {
76+
console.log(error);
77+
res.status(500).send("Error getting image");
78+
}
79+
});
80+
81+
app.delete("/image/:imageName", async (req, res) => {
82+
try {
83+
const imageName = req.params.imageName;
84+
const params = {
85+
Bucket: bucketName,
86+
Key: imageName,
87+
};
88+
89+
const command = new DeleteObjectCommand(params);
90+
91+
s3.send(command);
92+
93+
res.send("File deleted successfully!");
94+
} catch (error) {
95+
console.log(error);
96+
res.status(500).send("Error deleting image");
97+
}
98+
});
99+
100+
app.listen(port, () => {
101+
console.log(`Server is running on port ${port}`);
102+
});

controllers/file-server.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { getFile, deleteFile } = require("../services/aws/file-server");
2+
3+
const getFileFromAWS = async (req, res) => {
4+
try {
5+
const url = await getFile(req.params.fileName);
6+
// this url refers to the file in the AWS S3 bucket
7+
// I want to send a response that will show the image in the browser
8+
res.send(`<img src="${url}" alt="Image from AWS S3">`);
9+
} catch (error) {
10+
console.log(error);
11+
res.status(500).send("Error getting file");
12+
}
13+
};
14+
15+
const deleteFileFromAWS = async (req, res) => {
16+
try {
17+
deleteFile(req.params.fileName);
18+
res.send("File deleted successfully!");
19+
} catch (error) {
20+
console.log(error);
21+
res.status(500).send("Error deleting file");
22+
}
23+
};
24+
25+
module.exports = {
26+
getFileFromAWS,
27+
deleteFileFromAWS,
28+
};

controllers/video-content.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const crypto = require("crypto");
2+
const Video = require("../models/Video");
3+
const { getPrompt } = require("../services/text_service/prompt");
4+
const { getImagePrompts } = require("../services/image_service/image-prompts");
5+
const { getSpeech } = require("../services/audio_service/text-to-speech");
6+
const { getImage } = require("../services/image_service/image");
7+
const baseUrl = require("../utils/constants");
8+
9+
// console.log(baseUrl);
10+
11+
const createVideoContent = async (req, res) => {
12+
try {
13+
console.log("api hit, getting voice script");
14+
const { text } = req.body;
15+
16+
const videoID = crypto.randomBytes(16).toString("hex");
17+
18+
var voiceScripts = await getPrompt(text);
19+
var imagePrompts = await getImagePrompts(voiceScripts);
20+
voiceScripts = voiceScripts.replace(/,\s*([\]}])/g, "$1");
21+
imagePrompts = imagePrompts.replace(/,\s*([\]}])/g, "$1");
22+
23+
voiceScripts = JSON.parse(voiceScripts);
24+
25+
imagePrompts = JSON.parse(imagePrompts);
26+
voiceScripts = Object.values(voiceScripts);
27+
imagePrompts = Object.values(imagePrompts);
28+
29+
const imageUrls = [];
30+
const audioUrls = [];
31+
32+
for (let i = 0; i < imagePrompts.length; i++) {
33+
console.log("getting image " + i);
34+
getImage(imagePrompts[i], videoID + i);
35+
imageUrls.push(baseUrl + "/file/" + videoID + i + ".jpg");
36+
}
37+
38+
for (let i = 0; i < voiceScripts.length; i++) {
39+
console.log("getting audio " + i);
40+
const audioUrl = await getSpeech(voiceScripts[i], videoID + i);
41+
audioUrls.push(audioUrl);
42+
}
43+
44+
const video = new Video({
45+
videoID,
46+
voiceScripts,
47+
imagePrompts,
48+
imageUrls,
49+
audioUrls,
50+
});
51+
52+
await video.save();
53+
54+
setTimeout(() => {
55+
res.status(200).json(video);
56+
}, 20000);
57+
} catch (error) {
58+
console.log(error);
59+
res.status(500).send("Error creating video content");
60+
}
61+
};
62+
63+
const getVideoContentByID = async (req, res) => {
64+
try {
65+
const videoID = req.params.videoID;
66+
67+
const video = await Video.findOne({ videoID }).select("-_id -__v").exec();
68+
69+
if (!video) {
70+
return res.status(404).send("Video not found");
71+
}
72+
73+
res.status(200).json(video);
74+
} catch (error) {
75+
console.log(error);
76+
res.status(500).send("Error getting video content");
77+
}
78+
};
79+
80+
module.exports = { createVideoContent, getVideoContentByID };

db/database.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const mongoose = require("mongoose");
2+
const dbUrl = process.env.MONGO_URI;
3+
4+
mongoose
5+
.connect(dbUrl)
6+
.then(() => {
7+
console.log("Connected to database!");
8+
})
9+
.catch((error) => {
10+
console.log("Connection failed!", error);
11+
process.exit();
12+
});

models/Video.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const mongoose = require("mongoose");
2+
3+
const VideoSchema = new mongoose.Schema({
4+
videoID: {
5+
type: String,
6+
},
7+
voiceScripts: [
8+
{
9+
type: String,
10+
},
11+
],
12+
imageUrls: [
13+
{
14+
type: String,
15+
},
16+
],
17+
audioUrls: [
18+
{
19+
type: String,
20+
},
21+
],
22+
imageScripts: [
23+
{
24+
type: String,
25+
},
26+
],
27+
finalAudioUrl: {
28+
type: String,
29+
},
30+
31+
createdBy: {
32+
type: String,
33+
default: "sdsd",
34+
},
35+
36+
createdAt: {
37+
type: Date,
38+
default: Date.now,
39+
},
40+
});
41+
42+
const Video = mongoose.model("Video", VideoSchema);
43+
44+
module.exports = Video;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy