diff options
author | Jelle Besseling <jelle@pingiun.com> | 2021-08-17 08:26:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-17 08:26:20 +0200 |
commit | 0305db28c98fd6cf43a3c50ba92c76215e99d512 (patch) | |
tree | 33b753a19728d9f453c1aa4f19b36ac797e5fe80 /server/models/video/video.ts | |
parent | f88ae8f5bc223579313b28582de9101944a4a814 (diff) | |
download | PeerTube-0305db28c98fd6cf43a3c50ba92c76215e99d512.tar.gz PeerTube-0305db28c98fd6cf43a3c50ba92c76215e99d512.tar.zst PeerTube-0305db28c98fd6cf43a3c50ba92c76215e99d512.zip |
Add support for saving video files to object storage (#4290)
* Add support for saving video files to object storage
* Add support for custom url generation on s3 stored files
Uses two config keys to support url generation that doesn't directly go
to (compatible s3). Can be used to generate urls to any cache server or
CDN.
* Upload files to s3 concurrently and delete originals afterwards
* Only publish after move to object storage is complete
* Use base url instead of url template
* Fix mistyped config field
* Add rudenmentary way to download before transcode
* Implement Chocobozzz suggestions
https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478
The remarks in question:
Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names
Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket
Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET)
I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs)
https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer
I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs
* Import correct function
* Support multipart upload
* Remove import of node 15.0 module stream/promises
* Extend maximum upload job length
Using the same value as for redundancy downloading seems logical
* Use dynamic part size for really large uploads
Also adds very small part size for local testing
* Fix decreasePendingMove query
* Resolve various PR comments
* Move to object storage after optimize
* Make upload size configurable and increase default
* Prune webtorrent files that are stored in object storage
* Move files after transcoding jobs
* Fix federation
* Add video path manager
* Support move to external storage job in client
* Fix live object storage tests
Co-authored-by: Chocobozzz <me@florianbigard.com>
Diffstat (limited to 'server/models/video/video.ts')
-rw-r--r-- | server/models/video/video.ts | 51 |
1 files changed, 38 insertions, 13 deletions
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 56a5b0e18..874ad168a 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -28,14 +28,16 @@ import { buildNSFWFilter } from '@server/helpers/express-utils' | |||
28 | import { uuidToShort } from '@server/helpers/uuid' | 28 | import { uuidToShort } from '@server/helpers/uuid' |
29 | import { getPrivaciesForFederation, isPrivacyForFederation, isStateForFederation } from '@server/helpers/video' | 29 | import { getPrivaciesForFederation, isPrivacyForFederation, isStateForFederation } from '@server/helpers/video' |
30 | import { LiveManager } from '@server/lib/live/live-manager' | 30 | import { LiveManager } from '@server/lib/live/live-manager' |
31 | import { getHLSDirectory, getVideoFilePath } from '@server/lib/video-paths' | 31 | import { removeHLSObjectStorage, removeWebTorrentObjectStorage } from '@server/lib/object-storage' |
32 | import { getHLSDirectory, getHLSRedundancyDirectory } from '@server/lib/paths' | ||
33 | import { VideoPathManager } from '@server/lib/video-path-manager' | ||
32 | import { getServerActor } from '@server/models/application/application' | 34 | import { getServerActor } from '@server/models/application/application' |
33 | import { ModelCache } from '@server/models/model-cache' | 35 | import { ModelCache } from '@server/models/model-cache' |
34 | import { AttributesOnly, buildVideoEmbedPath, buildVideoWatchPath, pick } from '@shared/core-utils' | 36 | import { AttributesOnly, buildVideoEmbedPath, buildVideoWatchPath, pick } from '@shared/core-utils' |
35 | import { VideoFile } from '@shared/models/videos/video-file.model' | 37 | import { VideoFile } from '@shared/models/videos/video-file.model' |
36 | import { ResultList, UserRight, VideoPrivacy, VideoState } from '../../../shared' | 38 | import { ResultList, UserRight, VideoPrivacy, VideoState } from '../../../shared' |
37 | import { VideoObject } from '../../../shared/models/activitypub/objects' | 39 | import { VideoObject } from '../../../shared/models/activitypub/objects' |
38 | import { Video, VideoDetails, VideoRateType } from '../../../shared/models/videos' | 40 | import { Video, VideoDetails, VideoRateType, VideoStorage } from '../../../shared/models/videos' |
39 | import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' | 41 | import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' |
40 | import { VideoFilter } from '../../../shared/models/videos/video-query.type' | 42 | import { VideoFilter } from '../../../shared/models/videos/video-query.type' |
41 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' | 43 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' |
@@ -114,6 +116,7 @@ import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel | |||
114 | import { VideoCommentModel } from './video-comment' | 116 | import { VideoCommentModel } from './video-comment' |
115 | import { VideoFileModel } from './video-file' | 117 | import { VideoFileModel } from './video-file' |
116 | import { VideoImportModel } from './video-import' | 118 | import { VideoImportModel } from './video-import' |
119 | import { VideoJobInfoModel } from './video-job-info' | ||
117 | import { VideoLiveModel } from './video-live' | 120 | import { VideoLiveModel } from './video-live' |
118 | import { VideoPlaylistElementModel } from './video-playlist-element' | 121 | import { VideoPlaylistElementModel } from './video-playlist-element' |
119 | import { VideoShareModel } from './video-share' | 122 | import { VideoShareModel } from './video-share' |
@@ -732,6 +735,15 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> { | |||
732 | }) | 735 | }) |
733 | VideoCaptions: VideoCaptionModel[] | 736 | VideoCaptions: VideoCaptionModel[] |
734 | 737 | ||
738 | @HasOne(() => VideoJobInfoModel, { | ||
739 | foreignKey: { | ||
740 | name: 'videoId', | ||
741 | allowNull: false | ||
742 | }, | ||
743 | onDelete: 'cascade' | ||
744 | }) | ||
745 | VideoJobInfo: VideoJobInfoModel | ||
746 | |||
735 | @BeforeDestroy | 747 | @BeforeDestroy |
736 | static async sendDelete (instance: MVideoAccountLight, options) { | 748 | static async sendDelete (instance: MVideoAccountLight, options) { |
737 | if (!instance.isOwned()) return undefined | 749 | if (!instance.isOwned()) return undefined |
@@ -1641,9 +1653,10 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> { | |||
1641 | getMaxQualityResolution () { | 1653 | getMaxQualityResolution () { |
1642 | const file = this.getMaxQualityFile() | 1654 | const file = this.getMaxQualityFile() |
1643 | const videoOrPlaylist = file.getVideoOrStreamingPlaylist() | 1655 | const videoOrPlaylist = file.getVideoOrStreamingPlaylist() |
1644 | const originalFilePath = getVideoFilePath(videoOrPlaylist, file) | ||
1645 | 1656 | ||
1646 | return getVideoFileResolution(originalFilePath) | 1657 | return VideoPathManager.Instance.makeAvailableVideoFile(videoOrPlaylist, file, originalFilePath => { |
1658 | return getVideoFileResolution(originalFilePath) | ||
1659 | }) | ||
1647 | } | 1660 | } |
1648 | 1661 | ||
1649 | getDescriptionAPIPath () { | 1662 | getDescriptionAPIPath () { |
@@ -1673,16 +1686,24 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> { | |||
1673 | } | 1686 | } |
1674 | 1687 | ||
1675 | removeFileAndTorrent (videoFile: MVideoFile, isRedundancy = false) { | 1688 | removeFileAndTorrent (videoFile: MVideoFile, isRedundancy = false) { |
1676 | const filePath = getVideoFilePath(this, videoFile, isRedundancy) | 1689 | const filePath = isRedundancy |
1690 | ? VideoPathManager.Instance.getFSRedundancyVideoFilePath(this, videoFile) | ||
1691 | : VideoPathManager.Instance.getFSVideoFileOutputPath(this, videoFile) | ||
1677 | 1692 | ||
1678 | const promises: Promise<any>[] = [ remove(filePath) ] | 1693 | const promises: Promise<any>[] = [ remove(filePath) ] |
1679 | if (!isRedundancy) promises.push(videoFile.removeTorrent()) | 1694 | if (!isRedundancy) promises.push(videoFile.removeTorrent()) |
1680 | 1695 | ||
1696 | if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { | ||
1697 | promises.push(removeWebTorrentObjectStorage(videoFile)) | ||
1698 | } | ||
1699 | |||
1681 | return Promise.all(promises) | 1700 | return Promise.all(promises) |
1682 | } | 1701 | } |
1683 | 1702 | ||
1684 | async removeStreamingPlaylistFiles (streamingPlaylist: MStreamingPlaylist, isRedundancy = false) { | 1703 | async removeStreamingPlaylistFiles (streamingPlaylist: MStreamingPlaylist, isRedundancy = false) { |
1685 | const directoryPath = getHLSDirectory(this, isRedundancy) | 1704 | const directoryPath = isRedundancy |
1705 | ? getHLSRedundancyDirectory(this) | ||
1706 | : getHLSDirectory(this) | ||
1686 | 1707 | ||
1687 | await remove(directoryPath) | 1708 | await remove(directoryPath) |
1688 | 1709 | ||
@@ -1698,6 +1719,10 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> { | |||
1698 | await Promise.all( | 1719 | await Promise.all( |
1699 | streamingPlaylistWithFiles.VideoFiles.map(file => file.removeTorrent()) | 1720 | streamingPlaylistWithFiles.VideoFiles.map(file => file.removeTorrent()) |
1700 | ) | 1721 | ) |
1722 | |||
1723 | if (streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) { | ||
1724 | await removeHLSObjectStorage(streamingPlaylist, this) | ||
1725 | } | ||
1701 | } | 1726 | } |
1702 | } | 1727 | } |
1703 | 1728 | ||
@@ -1741,16 +1766,16 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> { | |||
1741 | this.privacy === VideoPrivacy.INTERNAL | 1766 | this.privacy === VideoPrivacy.INTERNAL |
1742 | } | 1767 | } |
1743 | 1768 | ||
1744 | async publishIfNeededAndSave (t: Transaction) { | 1769 | async setNewState (newState: VideoState, transaction: Transaction) { |
1745 | if (this.state !== VideoState.PUBLISHED) { | 1770 | if (this.state === newState) throw new Error('Cannot use same state ' + newState) |
1746 | this.state = VideoState.PUBLISHED | 1771 | |
1747 | this.publishedAt = new Date() | 1772 | this.state = newState |
1748 | await this.save({ transaction: t }) | ||
1749 | 1773 | ||
1750 | return true | 1774 | if (this.state === VideoState.PUBLISHED) { |
1775 | this.publishedAt = new Date() | ||
1751 | } | 1776 | } |
1752 | 1777 | ||
1753 | return false | 1778 | await this.save({ transaction }) |
1754 | } | 1779 | } |
1755 | 1780 | ||
1756 | getBandwidthBits (videoFile: MVideoFile) { | 1781 | getBandwidthBits (videoFile: MVideoFile) { |