Skip to content

Commit d8bf151

Browse files
committed
Merge branch 'develop' into 'master'
Develop See merge request opensource/plexify!9
2 parents dcf8955 + 6163d62 commit d8bf151

7 files changed

Lines changed: 923 additions & 1075 deletions

File tree

‎package-lock.json‎

Lines changed: 860 additions & 1029 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"scripts": {
1616
"build": "cli-tasks build",
1717
"build:docker": "cli-tasks build:docker",
18-
"plexify": "DEBUG=plexify:*:debug,info ts-node --files --pretty src/index",
18+
"plexify": "DEBUG=plexify:* ts-node --files --pretty src/index",
1919
"watch": "cli-tasks watch"
2020
},
2121
"husky": {

‎src/Commands/ScanCommand.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Arguments, CommandBuilder } from 'yargs'
22

3+
import { BError } from 'berror'
34
import { BarManager } from '../BarManager'
45
import { BaseCommand } from './BaseCommand'
56
import { ScanOptions } from '../Options/ScanOptions'
@@ -24,11 +25,15 @@ export class ScanCommand extends BaseCommand<ScanOptions> {
2425
}
2526

2627
handler = async (args: Arguments<ScanOptions>) => {
27-
const bars = new BarManager(args)
28-
const results = await this.scan(args, bars)
28+
try {
29+
const bars = new BarManager(args)
30+
const results = await this.scan(args, bars)
2931

30-
if (args.disableBars) {
31-
console.log(JSON.stringify(results))
32+
if (args.disableBars) {
33+
console.log(JSON.stringify(results))
34+
}
35+
} catch (error) {
36+
throw new BError('fatal', error)
3237
}
3338
}
3439
}

‎src/Logger.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { createLogger, createAwsTransformer, createScrubTransformer, createUriTransformer } from '@nnode/lincoln'
1+
import { createLogger, createScrubTransformer, createUriTransformer } from '@nnode/lincoln'
22
import { LincolnLogDebug } from '@nnode/lincoln-debug'
33

44
const logger = createLogger('plexify')
5-
logger.interceptors([createAwsTransformer, createScrubTransformer(['password']), createUriTransformer])
5+
logger.interceptors([createScrubTransformer(['password']), createUriTransformer])
66
LincolnLogDebug.observe(logger)
77

88
export const Logger = logger

‎src/MediaScanner.ts‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,30 @@ export class MediaScanner extends EventEmitter {
4242
}
4343

4444
async scan(path: string, minutes: number = 0, reverse: boolean = false, filter: MediaFileNameFilter = DefaultFilter) {
45-
this.log.info('[scan] gathering globs')
45+
const documents: MediaInfo[] = await this.media.all({
46+
selector: {
47+
'source.streams': {
48+
$elemMatch: {
49+
codec_name: {
50+
$ne: 'hevc',
51+
},
52+
codec_type: {
53+
$eq: 'video',
54+
},
55+
},
56+
},
57+
},
58+
fields: ['_id', 'filename'],
59+
})
60+
61+
this.log.info('[scan] retrieving cached', { cached: documents.length })
4662

63+
this.log.info('[scan] gathering globs')
4764
const unsorted = await fs.globs(this.globs, path)
48-
this.log.info('[scan] unsorted', { length: unsorted.length })
4965

66+
this.log.info('[scan] unsorted', { length: unsorted.length })
5067
const sorted = this.applySort(unsorted, reverse)
68+
5169
this.log.info('[scan] sorted', sorted.length, reverse)
5270

5371
const filtered = await this.applyAgeFilter(
@@ -62,22 +80,6 @@ export class MediaScanner extends EventEmitter {
6280

6381
this.emit(MediaScanner.events.start, total)
6482

65-
const documents = await this.media.all({
66-
selector: {
67-
'source.streams': {
68-
$elemMatch: {
69-
codec_name: {
70-
$ne: 'hevc',
71-
},
72-
codec_type: {
73-
$eq: 'video',
74-
},
75-
},
76-
},
77-
},
78-
fields: ['_id', 'filename'],
79-
})
80-
8183
const files = await Throttle(
8284
filtered.map((fullname, index) => async () => {
8385
try {
@@ -92,21 +94,21 @@ export class MediaScanner extends EventEmitter {
9294
}
9395
}
9496

95-
const info = await getMediaInfo(fullname)
97+
const source = await getMediaInfo(fullname)
9698

9799
const document: MediaInfo = this.media.document({
98100
filename,
99101
filepath,
100102
host: null,
101103
locked: false,
102-
source: info,
104+
source,
103105
})
104106

105107
await this.media.upsert(filename, document)
106108

107109
return this.convertable(document, index, total)
108110
} catch (error) {
109-
this.log.error(new BError('scan', error))
111+
this.log.error(new BError('scan', error), error)
110112
}
111113

112114
return null
@@ -187,10 +189,14 @@ export class MediaScanner extends EventEmitter {
187189
}
188190

189191
private findAudioStream(data: FfprobeData): FfprobeStream {
190-
return data.streams.filter((stream) => stream.codec_type === 'audio').reduce((_, current) => current)
192+
return data.streams
193+
.filter((stream) => stream.codec_type === 'audio')
194+
.reduce<FfprobeStream | any>((_, current) => current, {})
191195
}
192196

193197
private findVideoStream(data: FfprobeData): FfprobeStream {
194-
return data.streams.filter((stream) => stream.codec_type === 'video').reduce((_, current) => current)
198+
return data.streams
199+
.filter((stream) => stream.codec_type === 'video')
200+
.reduce<FfprobeStream | any>((_, current) => current, {})
195201
}
196202
}

‎src/MediaStore.ts‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,54 @@ export class MediaStore {
2222
private readonly log: Lincoln
2323

2424
constructor(logger: Lincoln) {
25-
const options = {
25+
const options: PouchDB.Configuration.RemoteDatabaseConfiguration = {
2626
adapter: 'http',
2727
auth: {
2828
password: process.env.PLEXIFY_COUCHDB_PASSWORD || 'guest',
2929
username: process.env.PLEXIFY_COUCHDB_USERNAME || 'guest',
3030
},
31-
name: COUCHDB_URL,
31+
name: fs.basename(COUCHDB_URL),
3232
}
3333

3434
this.database = new PouchDB<MediaInfo>(COUCHDB_URL, options)
3535
this.log = logger.extend('storage')
36-
this.log.trace(options)
36+
this.log.trace('options', options)
3737
}
3838

3939
async all(options?: PouchDB.Find.FindRequest<MediaInfo>) {
4040
try {
4141
const results = await this.database.find(options || { selector: {} })
4242
return results.docs
4343
} catch (error) {
44-
this.log.error(new BError('all', error))
44+
this.log.error(new BError('all', error), error)
4545
return []
4646
}
4747
}
4848

49+
dbinfo() {
50+
return this.database.info()
51+
}
52+
4953
document(doc: Partial<MediaInfo>): MediaInfo {
50-
return { ...doc, _id: this.cleanid(doc.filename!) } as MediaInfo
54+
const document: MediaInfo = { ...doc, _id: this.cleanid(doc.filename!) } as MediaInfo
55+
return document
5156
}
5257

5358
async exists(id: string) {
5459
try {
55-
const document = await this.database.get<MediaInfo>(id)
60+
const document = await this.database.get(id)
5661
return document._id === this.cleanid(id)
5762
} catch (error) {
58-
this.log.error(new BError('exists', error))
63+
this.log.error(new BError('exists', error), error)
5964
return false
6065
}
6166
}
6267

6368
async get(id: string) {
6469
try {
65-
return await this.database.get<MediaInfo>(this.cleanid(id))
70+
return await this.database.get(this.cleanid(id))
6671
} catch (error) {
67-
this.log.error(new BError('get', error))
72+
this.log.error(new BError('get', error), error)
6873
return null
6974
}
7075
}
@@ -81,16 +86,16 @@ export class MediaStore {
8186
try {
8287
return await this.upsert(this.cleanid(id), { source, ...{ locked: true, host: os.hostname() } })
8388
} catch (error) {
84-
this.log.error(new BError('lock', error))
89+
this.log.error(new BError('lock', error), error)
8590
}
8691
}
8792

8893
async locked(id: string) {
8994
try {
90-
const document = await this.database.get<MediaInfo>(this.cleanid(id))
95+
const document = await this.database.get(this.cleanid(id))
9196
return document.locked === true && document.host !== os.hostname()
9297
} catch (error) {
93-
this.log.error(new BError('locked', error))
98+
this.log.error(new BError('locked', error), error)
9499
return false
95100
}
96101
}
@@ -100,15 +105,16 @@ export class MediaStore {
100105
this.log.trace('unlocked', this.cleanid(id))
101106
return await this.upsert(this.cleanid(id), { source, ...{ locked: false, host: null } })
102107
} catch (error) {
103-
this.log.error(new BError('unlock', error))
108+
this.log.error(new BError('unlock', error), error)
104109
}
105110
}
106111

107112
async upsert(id: string, document: Partial<MediaInfo>) {
108-
const results = await this.database.upsert<MediaInfo>(
113+
const results = await this.database.upsert(
109114
this.cleanid(id),
110115
(target: MediaInfo) =>
111116
({
117+
...{ _id: this.cleanid(id) },
112118
...target,
113119
...document,
114120
} as MediaInfo),
@@ -118,7 +124,7 @@ export class MediaStore {
118124
}
119125

120126
private cleanid(id: string): string {
121-
return btoa(this.ident(id))
127+
return Buffer.from(this.ident(id)).toString('hex')
122128
}
123129

124130
private ident(id: string) {

‎src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function main() {
1717
.config(fs.join(filepath, 'plexify.json'))
1818
.parse()
1919

20-
Logger.trace(args)
20+
Logger.trace('args', args)
2121
}
2222

2323
main().catch((error) => Logger.error(new BError('catch-all', error)))

0 commit comments

Comments
 (0)