Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Prevent unnecessary torrent filename update

Chocobozzz26c54658580c
diff --git a/packages/tests/src/api/videos/multiple-servers.ts b/packages/tests/src/api/videos/multiple-servers.ts
index 97f8850595..2e77894d04 100644
--- a/packages/tests/src/api/videos/multiple-servers.ts
+++ b/packages/tests/src/api/videos/multiple-servers.ts
@@ -479,6 +479,20 @@ describe('Test multiple servers', function () {
let remoteVideosServer2 = []
let remoteVideosServer3 = []
+ let fileUrls: string[] = []
+
+ async function grabFileUrls (videoUUID: string) {
+ const video = await servers[0].videos.get({ id: videoUUID })
+ const { storyboards } = await servers[0].storyboard.list({ id: videoUUID })
+
+ return [
+ ...video.files.map(f => f.fileUrl),
+ ...video.files.map(f => f.torrentUrl),
+ ...video.thumbnails.map(f => f.fileUrl),
+ ...storyboards.map(s => s.fileUrl)
+ ]
+ }
+
before(async function () {
{
const { data } = await servers[0].videos.list()
@@ -495,6 +509,8 @@ describe('Test multiple servers', function () {
localVideosServer3 = data.filter(video => video.isLocal === true).map(video => video.uuid)
remoteVideosServer3 = data.filter(video => video.isLocal === false).map(video => video.uuid)
}
+
+ fileUrls = await grabFileUrls(localVideosServer3[0])
})
it('Should view multiple videos on owned servers', async function () {
@@ -609,6 +625,12 @@ describe('Test multiple servers', function () {
}
}
})
+
+ it('Should not have updated torrent files/storyboards/thumbnails', async function () {
+ const newFileUrls = await grabFileUrls(localVideosServer3[0])
+
+ expect(newFileUrls).to.have.members(fileUrls)
+ })
})
describe('Should manipulate these videos', function () {
diff --git a/packages/tests/src/shared/p2p.ts b/packages/tests/src/shared/p2p.ts
index b5c7aa6fc9..e03a93fb87 100644
--- a/packages/tests/src/shared/p2p.ts
+++ b/packages/tests/src/shared/p2p.ts
@@ -6,11 +6,10 @@ import { readFile } from 'fs/promises'
import type { Instance as MagnetUriInstance } from 'magnet-uri'
import type { ParseTorrent } from 'parse-torrent'
import { basename, join } from 'path'
-import type { Torrent } from 'webtorrent'
-import WebTorrent from 'webtorrent'
+import type { Torrent, Instance, WebTorrent } from 'webtorrent'
export async function checkWebTorrentWorks (magnetUri: string, pathMatch?: RegExp) {
- let res: { webtorrent: WebTorrent.Instance, torrent: WebTorrent.Torrent }
+ let res: { webtorrent: Instance, torrent: Torrent }
try {
res = await webtorrentAdd(magnetUri)
@@ -56,13 +55,14 @@ export async function magnetUriEncode (data: MagnetUriInstance) {
// ---------------------------------------------------------------------------
async function webtorrentAdd (torrentId: string) {
- const WebTorrent = (await import('webtorrent')).default
+ const WebTorrent: WebTorrent = (await import('webtorrent')).default
const webtorrent = new WebTorrent({
natUpnp: false,
natPmp: false,
utp: false,
- lsd: false
+ lsd: false,
+ dht: false
} as any)
webtorrent.on('error', err => console.error('Error in webtorrent', err))
diff --git a/server/core/lib/activitypub/videos/shared/abstract-builder.ts b/server/core/lib/activitypub/videos/shared/abstract-builder.ts
index 6944660dd8..2d355513c5 100644
--- a/server/core/lib/activitypub/videos/shared/abstract-builder.ts
+++ b/server/core/lib/activitypub/videos/shared/abstract-builder.ts
@@ -110,13 +110,16 @@ export abstract class APVideoAbstractBuilder {
}
protected async insertOrReplaceStoryboard (video: MVideoFullLight, t: Transaction) {
+ const storyboardAttributes = getStoryboardAttributeFromObject(video, this.videoObject)
+
const existingStoryboard = await StoryboardModel.loadByVideo(video.id, t)
- if (existingStoryboard) await existingStoryboard.destroy({ transaction: t })
+ if (existingStoryboard?.fileUrl === storyboardAttributes?.fileUrl) return
- const storyboardAttributes = getStoryboardAttributeFromObject(video, this.videoObject)
- if (!storyboardAttributes) return
+ if (existingStoryboard) await existingStoryboard.destroy({ transaction: t })
- return StoryboardModel.create(storyboardAttributes, { transaction: t })
+ if (storyboardAttributes) {
+ await StoryboardModel.create(storyboardAttributes, { transaction: t })
+ }
}
protected async insertOrReplaceLive (video: MVideoFullLight, transaction: Transaction) {
@@ -134,11 +137,12 @@ export abstract class APVideoAbstractBuilder {
}
protected async setWebVideoFiles (video: MVideoFullLight, t: Transaction) {
- const videoFileAttributes = getFileAttributesFromUrl(video, this.videoObject.url)
- const newVideoFiles = videoFileAttributes.map(a => new VideoFileModel(a))
+ const oldFiles = video.VideoFiles || []
+
+ const newVideoFiles = getFileAttributesFromUrl(video, this.videoObject.url, oldFiles).map(a => new VideoFileModel(a))
// Remove video files that do not exist anymore
- await deleteAllModels(filterNonExistingModels(video.VideoFiles || [], newVideoFiles), t)
+ await deleteAllModels(filterNonExistingModels(oldFiles, newVideoFiles), t)
// Update or add other one
const upsertTasks = newVideoFiles.map(f => VideoFileModel.customUpsert(f, 'video', t))
@@ -217,7 +221,11 @@ export abstract class APVideoAbstractBuilder {
) {
const oldStreamingPlaylistFiles = this.getStreamingPlaylistFiles(oldPlaylists || [], playlistModel.type)
- const newVideoFiles: MVideoFile[] = getFileAttributesFromUrl(playlistModel, tagObjects).map(a => new VideoFileModel(a))
+ const newVideoFiles: MVideoFile[] = getFileAttributesFromUrl(
+ playlistModel,
+ tagObjects,
+ oldStreamingPlaylistFiles
+ ).map(a => new VideoFileModel(a))
await deleteAllModels(filterNonExistingModels(oldStreamingPlaylistFiles, newVideoFiles), t)
diff --git a/server/core/lib/activitypub/videos/shared/object-to-model-attributes.ts b/server/core/lib/activitypub/videos/shared/object-to-model-attributes.ts
index b6059897cf..6959bd36b2 100644
--- a/server/core/lib/activitypub/videos/shared/object-to-model-attributes.ts
+++ b/server/core/lib/activitypub/videos/shared/object-to-model-attributes.ts
@@ -54,54 +54,57 @@ export function getTagsFromObject (videoObject: VideoObject) {
export function getFileAttributesFromUrl (
videoOrPlaylist: MVideo | MStreamingPlaylistVideo,
- urls: (ActivityTagObject | ActivityUrlObject)[]
+ urls: (ActivityTagObject | ActivityUrlObject)[],
+ oldFiles: MVideoFile[]
) {
- const fileUrls = urls.filter(u => isAPVideoUrlObject(u))
- if (fileUrls.length === 0) return []
+ const fileUrlObjects = urls.filter(u => isAPVideoUrlObject(u))
+ if (fileUrlObjects.length === 0) return []
const attributes: FilteredModelAttributes<VideoFileModel>[] = []
- for (const fileUrl of fileUrls) {
+ for (const fileUrlObject of fileUrlObjects) {
// Fetch associated metadata url, if any
const metadata = urls.filter(isAPVideoFileUrlMetadataObject)
.find(u => {
- return u.height === fileUrl.height &&
- u.fps === fileUrl.fps &&
- u.rel.includes(fileUrl.mediaType)
+ return u.height === fileUrlObject.height &&
+ u.fps === fileUrlObject.fps &&
+ u.rel.includes(fileUrlObject.mediaType)
})
- const extname = getExtFromMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT, fileUrl.mediaType)
- const resolution = fileUrl.height
+ const extname = getExtFromMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT, fileUrlObject.mediaType)
+ const resolution = fileUrlObject.height
const [ videoId, videoStreamingPlaylistId ] = isStreamingPlaylist(videoOrPlaylist)
? [ null, videoOrPlaylist.id ]
: [ videoOrPlaylist.id, null ]
- const { torrentFilename, infoHash, torrentUrl } = getTorrentRelatedInfo({ videoOrPlaylist, urls, fileUrl })
+ const fileUrl = fileUrlObject.href
+ const existingFile = oldFiles.find(f => f.fileUrl === fileUrl)
+ const { torrentFilename, infoHash, torrentUrl } = getTorrentRelatedInfo({ videoOrPlaylist, urls, fileUrlObject, existingFile })
const attribute: Partial<AttributesOnly<MVideoFile>> = {
extname,
resolution,
- size: fileUrl.size,
- fps: exists(fileUrl.fps) && fileUrl.fps >= 0
- ? fileUrl.fps
+ size: fileUrlObject.size,
+ fps: exists(fileUrlObject.fps) && fileUrlObject.fps >= 0
+ ? fileUrlObject.fps
: -1,
metadataUrl: metadata?.href,
- width: fileUrl.width,
- height: fileUrl.height,
+ width: fileUrlObject.width,
+ height: fileUrlObject.height,
// Use the name of the remote file because we don't proxify video file requests
- filename: basename(fileUrl.href),
- fileUrl: fileUrl.href,
+ filename: basename(fileUrl),
+ fileUrl,
infoHash,
torrentFilename,
torrentUrl,
- formatFlags: buildFileFormatFlags(fileUrl, isStreamingPlaylist(videoOrPlaylist)),
- streams: buildFileStreams(fileUrl, resolution),
+ formatFlags: buildFileFormatFlags(fileUrlObject, isStreamingPlaylist(videoOrPlaylist)),
+ streams: buildFileStreams(fileUrlObject, resolution),
// This is a video file owned by a video or by a streaming playlist
videoId,
@@ -348,13 +351,14 @@ function isAPSensitiveTagObject (tag: any): tag is ActivitySensitiveTagObject {
function getTorrentRelatedInfo (options: {
videoOrPlaylist: MVideo | MStreamingPlaylistVideo
urls: (ActivityTagObject | ActivityUrlObject)[]
- fileUrl: ActivityVideoUrlObject
+ fileUrlObject: ActivityVideoUrlObject
+ existingFile?: MVideoFile
}) {
- const { urls, fileUrl, videoOrPlaylist } = options
+ const { urls, fileUrlObject, videoOrPlaylist, existingFile } = options
// Fetch associated magnet uri
const magnet = urls.filter(isAPMagnetUrlObject)
- .find(u => u.height === fileUrl.height)
+ .find(u => u.height === fileUrlObject.height)
if (!magnet) {
return {
@@ -377,7 +381,7 @@ function getTorrentRelatedInfo (options: {
torrentUrl,
// Use our own torrent name since we proxify torrent requests
- torrentFilename: generateTorrentFileName(videoOrPlaylist, fileUrl.height),
+ torrentFilename: existingFile?.torrentFilename ?? generateTorrentFileName(videoOrPlaylist, fileUrlObject.height),
infoHash: magnetParsed.infoHash
}