-
-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Newsfeed management Updated and required changes (#1383)
* implementation of the newsfeed feature * implementation of the newsfeed feature
- Loading branch information
1 parent
810457b
commit 3973d69
Showing
26 changed files
with
414 additions
and
380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Types, Model } from "mongoose"; | ||
import { Schema, model, models } from "mongoose"; | ||
/** | ||
* This is an interface that represents a database(MongoDB) document for Encoded Video. | ||
*/ | ||
export interface InterfaceEncodedVideo { | ||
_id: Types.ObjectId; | ||
fileName: string; | ||
content: string; | ||
numberOfUses: number; | ||
} | ||
/** | ||
* This describes the schema for a `encodedVideo` that corresponds to `InterfaceEncodedVideo` document. | ||
* @param fileName - File name. | ||
* @param content - Content. | ||
* @param numberOfUses - Number of Uses. | ||
*/ | ||
const encodedVideoSchema = new Schema({ | ||
fileName: { | ||
type: String, | ||
required: true, | ||
}, | ||
content: { | ||
type: String, | ||
required: true, | ||
}, | ||
numberOfUses: { | ||
type: Number, | ||
required: true, | ||
default: 1, | ||
}, | ||
}); | ||
|
||
const encodedVideoModel = (): Model<InterfaceEncodedVideo> => | ||
model<InterfaceEncodedVideo>("EncodedVideo", encodedVideoSchema); | ||
|
||
// This syntax is needed to prevent Mongoose OverwriteModelError while running tests. | ||
export const EncodedVideo = (models.EncodedVideo || | ||
encodedVideoModel()) as ReturnType<typeof encodedVideoModel>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { unlink } from "fs/promises"; | ||
import path from "path"; | ||
import { EncodedVideo } from "../../models/EncodedVideo"; | ||
|
||
export const deletePreviousVideo = async ( | ||
videoToBeDeletedPath: string | ||
): Promise<void> => { | ||
const videoToBeDeleted = await EncodedVideo.findOne({ | ||
fileName: videoToBeDeletedPath!, | ||
}); | ||
|
||
if (videoToBeDeleted?.numberOfUses === 1) { | ||
await unlink(path.join(__dirname, "../../../" + videoToBeDeleted.fileName)); | ||
await EncodedVideo.deleteOne({ | ||
fileName: videoToBeDeletedPath, | ||
}); | ||
} | ||
|
||
await EncodedVideo.findOneAndUpdate( | ||
{ | ||
fileName: videoToBeDeletedPath, | ||
}, | ||
{ | ||
$inc: { | ||
numberOfUses: -1, | ||
}, | ||
} | ||
); | ||
}; |
15 changes: 15 additions & 0 deletions
15
src/utilities/encodedVideoStorage/encodedVideoExtensionCheck.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const encodedVideoExtentionCheck = (encodedUrl: string): boolean => { | ||
const extension = encodedUrl.substring( | ||
"data:".length, | ||
encodedUrl.indexOf(";base64") | ||
); | ||
|
||
console.log(extension); | ||
|
||
const isValidVideo = extension === "video/mp4"; | ||
if (isValidVideo) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import shortid from "shortid"; | ||
import * as fs from "fs"; | ||
import { writeFile } from "fs/promises"; | ||
import { encodedVideoExtentionCheck } from "./encodedVideoExtensionCheck"; | ||
import { errors, requestContext } from "../../libraries"; | ||
import { INVALID_FILE_TYPE } from "../../constants"; | ||
import { EncodedVideo } from "../../models/EncodedVideo"; | ||
import path from "path"; | ||
import { deletePreviousVideo } from "./deletePreviousVideo"; | ||
|
||
export const uploadEncodedVideo = async ( | ||
encodedVideoURL: string, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
previousVideoPath?: string | null | ||
): Promise<string> => { | ||
const isURLValidVideo = encodedVideoExtentionCheck(encodedVideoURL); | ||
|
||
if (!isURLValidVideo) { | ||
throw new errors.InvalidFileTypeError( | ||
requestContext.translate(INVALID_FILE_TYPE.MESSAGE), | ||
INVALID_FILE_TYPE.CODE, | ||
INVALID_FILE_TYPE.PARAM | ||
); | ||
} | ||
|
||
const encodedVideoAlreadyExist = await EncodedVideo.findOne({ | ||
content: encodedVideoURL, | ||
}); | ||
|
||
if (previousVideoPath) { | ||
await deletePreviousVideo(previousVideoPath); | ||
} | ||
|
||
if (encodedVideoAlreadyExist) { | ||
await EncodedVideo.findOneAndUpdate( | ||
{ | ||
content: encodedVideoURL, | ||
}, | ||
{ | ||
$inc: { | ||
numberOfUses: 1, | ||
}, | ||
} | ||
); | ||
return encodedVideoAlreadyExist.fileName; | ||
} | ||
|
||
let id = shortid.generate(); | ||
|
||
id = "videos/" + id + "video.mp4"; | ||
|
||
const uploadedEncodedVideo = await EncodedVideo.create({ | ||
fileName: id, | ||
content: encodedVideoURL, | ||
}); | ||
|
||
const data = encodedVideoURL.replace(/^data:video\/\w+;base64,/, ""); | ||
|
||
const buf = Buffer.from(data, "base64"); | ||
|
||
if (!fs.existsSync(path.join(__dirname, "../../../videos"))) { | ||
fs.mkdir(path.join(__dirname, "../../../videos"), (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
}); | ||
} | ||
|
||
await writeFile(path.join(__dirname, "../../../" + id), buf); | ||
|
||
return uploadedEncodedVideo.fileName; | ||
}; |
Oops, something went wrong.