-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #429 from BinaryStudioAcademy/task/OV-377-create-p…
…review-page OV-377: Create preview page and flow
- Loading branch information
Showing
38 changed files
with
488 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { VideosApiPath, VideoValidationMessage } from 'shared'; |
5 changes: 5 additions & 0 deletions
5
backend/src/bundles/public-video/enums/public-videos-api-path.enum.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,5 @@ | ||
const PublicVideosApiPath = { | ||
ROOT: '/', | ||
} as const; | ||
|
||
export { PublicVideosApiPath }; |
42 changes: 42 additions & 0 deletions
42
backend/src/bundles/public-video/public-video.controller.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,42 @@ | ||
import { type PublicVideoService } from '~/bundles/public-video/public-video.service.js'; | ||
import { | ||
type ApiHandlerOptions, | ||
type ApiHandlerResponse, | ||
BaseController, | ||
} from '~/common/controller/controller.js'; | ||
import { ApiPath } from '~/common/enums/enums.js'; | ||
import { HTTPCode, HTTPMethod } from '~/common/http/http.js'; | ||
import { type Logger } from '~/common/logger/logger.js'; | ||
|
||
import { PublicVideosApiPath } from './enums/public-videos-api-path.enum.js'; | ||
|
||
class PublicVideoController extends BaseController { | ||
private publicVideoService: PublicVideoService; | ||
|
||
public constructor(logger: Logger, publicVideoService: PublicVideoService) { | ||
super(logger, ApiPath.PUBLIC_VIDEO); | ||
|
||
this.publicVideoService = publicVideoService; | ||
|
||
this.addRoute({ | ||
path: PublicVideosApiPath.ROOT, | ||
method: HTTPMethod.GET, | ||
handler: (options) => this.findUrlByToken(options), | ||
}); | ||
} | ||
|
||
private async findUrlByToken( | ||
options: ApiHandlerOptions, | ||
): Promise<ApiHandlerResponse> { | ||
const headers = options.headers as Record<string, { value: string }>; | ||
const videoTokenHeader = headers['video_token']?.toString() ?? ''; | ||
|
||
return { | ||
status: HTTPCode.OK, | ||
payload: | ||
await this.publicVideoService.findUrlByToken(videoTokenHeader), | ||
}; | ||
} | ||
} | ||
|
||
export { PublicVideoController }; |
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,41 @@ | ||
import { type VideoRepository } from '~/bundles/videos/video.repository.js'; | ||
import { HTTPCode, HttpError } from '~/common/http/http.js'; | ||
import { tokenService } from '~/common/services/services.js'; | ||
|
||
import { VideoValidationMessage } from './enums/enums.js'; | ||
|
||
class PublicVideoService { | ||
private videoRepository: VideoRepository; | ||
public constructor(videoRepository: VideoRepository) { | ||
this.videoRepository = videoRepository; | ||
} | ||
|
||
public async findUrlByToken(token: string): Promise<string> { | ||
const id = await tokenService.getIdFromToken(token); | ||
|
||
if (!id) { | ||
this.throwVideoNotFoundError(); | ||
} | ||
|
||
const video = await this.videoRepository.findById(id); | ||
|
||
if (!video) { | ||
this.throwVideoNotFoundError(); | ||
} | ||
|
||
const { url } = video.toObject(); | ||
if (!url) { | ||
this.throwVideoNotFoundError(); | ||
} | ||
return url; | ||
} | ||
|
||
private throwVideoNotFoundError(): never { | ||
throw new HttpError({ | ||
message: VideoValidationMessage.VIDEO_DOESNT_EXIST, | ||
status: HTTPCode.NOT_FOUND, | ||
}); | ||
} | ||
} | ||
|
||
export { PublicVideoService }; |
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,13 @@ | ||
import { VideoModel } from '~/bundles/videos/video.model.js'; | ||
import { VideoRepository } from '~/bundles/videos/video.repository.js'; | ||
import { logger } from '~/common/logger/logger.js'; | ||
import { imageService } from '~/common/services/services.js'; | ||
|
||
import { PublicVideoController } from './public-video.controller.js'; | ||
import { PublicVideoService } from './public-video.service.js'; | ||
|
||
const videoRepository = new VideoRepository(VideoModel, imageService); | ||
const videoService = new PublicVideoService(videoRepository); | ||
const publicVideoController = new PublicVideoController(logger, videoService); | ||
|
||
export { publicVideoController }; |
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
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
1 change: 1 addition & 0 deletions
1
frontend/src/bundles/common/api/public-video-api/enums/enums.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 @@ | ||
export { VideosApiPath } from 'shared'; |
44 changes: 44 additions & 0 deletions
44
frontend/src/bundles/common/api/public-video-api/public-videos-api.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,44 @@ | ||
import { ApiPath, ContentType } from '~/bundles/common/enums/enums.js'; | ||
import { type Http, HTTPMethod } from '~/framework/http/http.js'; | ||
import { BaseHttpApi } from '~/framework/http-api/http-api.js'; | ||
import { type Storage } from '~/framework/storage/storage.js'; | ||
|
||
import { VideosApiPath } from './enums/enums.js'; | ||
|
||
type Constructor = { | ||
baseUrl: string; | ||
http: Http; | ||
storage: Storage; | ||
}; | ||
|
||
class PublicVideosApi extends BaseHttpApi { | ||
public constructor({ baseUrl, http, storage }: Constructor) { | ||
super({ path: ApiPath.PUBLIC_VIDEO, baseUrl, http, storage }); | ||
} | ||
|
||
public async getVideoUrlFromJWT(jwt: string): Promise<string> { | ||
const headers = new Headers(); | ||
headers.append('video_token', jwt.replaceAll('~', '.')); | ||
|
||
const options = { | ||
method: HTTPMethod.GET, | ||
contentType: ContentType.JSON, | ||
hasAuth: true, | ||
customHeaders: headers, | ||
}; | ||
|
||
const response = await this.load( | ||
this.getFullEndpoint(`${VideosApiPath.ROOT}`, {}), | ||
options, | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error( | ||
`Failed to get video ID JWT: ${response.statusText}`, | ||
); | ||
} | ||
return await response.text(); | ||
} | ||
} | ||
|
||
export { PublicVideosApi }; |
Oops, something went wrong.