Repository navigation
Expand file tree
/
Copy pathMediaStore.ts
More file actions
160 lines (133 loc) · 3.99 KB
/
Copy pathMediaStore.ts
File metadata and controls
160 lines (133 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os from 'os'
import md5 from 'md5'
import PouchDB from 'pouchdb'
import Find from 'pouchdb-find'
import Upsert from 'pouchdb-upsert'
import { BError } from 'berror'
import { fs } from '@nofrills/fs'
import { Lincoln } from '@nnode/lincoln'
import { FfprobeData } from 'fluent-ffmpeg'
import { MediaInfo } from './MediaInfo'
PouchDB.plugin(Find)
PouchDB.plugin(Upsert)
const COUCHDB_URL = process.env.PLEXIFY_COUCHDB_URL || 'http://localhost/plexify'
export class MediaStore {
readonly database: PouchDB.Database<MediaInfo>
private readonly log: Lincoln
constructor(logger: Lincoln) {
const options: PouchDB.Configuration.RemoteDatabaseConfiguration = {
adapter: 'http',
auth: {
password: process.env.PLEXIFY_COUCHDB_PASSWORD || 'guest',
username: process.env.PLEXIFY_COUCHDB_USERNAME || 'guest',
},
name: fs.basename(COUCHDB_URL),
}
this.database = new PouchDB<MediaInfo>(COUCHDB_URL, options)
this.log = logger.extend('storage')
this.log.trace('options', options)
}
async all(options?: PouchDB.Find.FindRequest<MediaInfo>) {
try {
const results = await this.database.find(options || { selector: {} })
return results.docs
} catch (error) {
this.log.error(new BError('all', error), error)
return []
}
}
bulk(batch: MediaInfo[]) {
return this.database.bulkDocs(batch)
}
dbinfo() {
return this.database.info()
}
document(doc: Partial<MediaInfo>): MediaInfo {
const document: MediaInfo = { ...doc, _id: this.cleanid(doc.filename!) } as MediaInfo
return document
}
async exists(id: string) {
try {
const key = this.cleanid(id)
const document = await this.database.get(key)
const exists = document._id === key
this.log.trace('exists', { id, docid: document._id, exists })
return exists
} catch (error) {
this.log.error(new BError('exists', error), error)
return false
}
}
async get(id: string) {
try {
return await this.database.get(this.cleanid(id))
} catch (error) {
this.log.error(new BError('get', error), error)
return null
}
}
has(id: string, set: MediaInfo[]) {
return set.map((x) => x._id).includes(this.cleanid(id))
}
async lock(id: string, source: FfprobeData) {
try {
const document = await this.get(this.cleanid(id))
if (document) {
document.host = os.hostname()
document.locked = true
document.source = source
await this.upsert(document._id, document)
this.log.trace('locked', { id, document })
}
} catch (error) {
this.log.error(new BError('lock', error), error)
}
}
async locked(id: string) {
try {
const document = await this.database.get(this.cleanid(id))
return document.locked === true && document.host !== os.hostname()
} catch (error) {
this.log.error(new BError('locked', error), error)
return false
}
}
async unlock(id: string, source: FfprobeData) {
try {
const document = await this.get(this.cleanid(id))
if (document) {
document.host = null
document.locked = false
document.source = source
await this.upsert(document._id, document)
this.log.trace('unlocked', { id, document })
}
} catch (error) {
this.log.error(new BError('unlock', error), error)
}
}
async upsert(id: string, document: Partial<MediaInfo>) {
const key = this.cleanid(id)
const response = await this.database.upsert(key, (target: MediaInfo) => ({
...target,
...document,
}))
return response
}
private cleanid(id: string): string {
if (id.startsWith('id-')) {
return id
}
const hash = md5(this.fileident(id))
this.log.trace('cleanid', { id, hash })
return `id-${hash}`
}
private fileident(id: string) {
if (id.startsWith('id-')) {
return id
}
const ident = fs.basename(id, false)
this.log.trace('fileident', { id, ident })
return ident
}
}