@@ -5,6 +5,8 @@ import { Controller, Get, Res, Req, Param, Logger, Inject } from '@nestjs/common
5
5
import { ConfigService } from '../shared/config/config.service'
6
6
import { TorrentService } from '../shared/torrent/torrent.service'
7
7
8
+ export type Files = [ { stats : fs . Stats , fileName : string } ]
9
+
8
10
@Controller ( )
9
11
export class WatchController {
10
12
@@ -24,14 +26,17 @@ export class WatchController {
24
26
const filesInDirectory = fs . readdirSync ( dir , { withFileTypes : true } )
25
27
26
28
const files = filesInDirectory . map ( ( file ) => {
27
- const res = path . resolve ( dir , file . name )
29
+ const fileName = path . resolve ( dir , file . name )
28
30
29
31
return file . isDirectory ( )
30
- ? this . getFiles ( res )
31
- : res
32
+ ? this . getFiles ( fileName )
33
+ : {
34
+ stats : fs . statSync ( fileName ) ,
35
+ fileName
36
+ }
32
37
} )
33
38
34
- return Array . prototype . concat ( ...files )
39
+ return Array . prototype . concat ( ...files ) as Files
35
40
}
36
41
37
42
@Get ( 'watch/:_id' )
@@ -57,13 +62,13 @@ export class WatchController {
57
62
}
58
63
59
64
// Get the correct media file
60
- const mediaFile = files . reduce ( ( previous , current , index ) => {
61
- const formatIsSupported = ! ! this . torrentService . supportedFormats . find ( format => (
62
- current . includes ( format ) && ! current . includes ( 'transcoding' )
65
+ const mediaFile = files . reduce ( ( previous , current ) => {
66
+ const formatIsSupported = ! ! this . torrentService . supportedFormats . find ( ( format ) => (
67
+ current . fileName . includes ( format )
63
68
) )
64
69
65
70
if ( formatIsSupported ) {
66
- if ( ! previous || current . length > previous . length ) {
71
+ if ( ! previous || current . stats . size > previous . stats . size ) {
67
72
return current
68
73
}
69
74
}
@@ -84,7 +89,7 @@ export class WatchController {
84
89
const isChromeCast = req ?. query ?. device === 'chromecast' ?? false
85
90
86
91
// Get the size of the media file
87
- let { size : mediaSize } = fs . statSync ( mediaFile )
92
+ let mediaSize = mediaFile . stats . size
88
93
89
94
// If it its Chromecast and we have a torrent then use the file size from that
90
95
if ( isChromeCast && torrent ) {
@@ -135,7 +140,7 @@ export class WatchController {
135
140
torrent . file . createReadStream ( streamOptions )
136
141
}
137
142
138
- res . send ( fs . createReadStream ( mediaFile , streamOptions ) )
143
+ res . send ( fs . createReadStream ( mediaFile . fileName , streamOptions ) )
139
144
}
140
145
141
146
}
0 commit comments