2022-04-03 19:16:17 -05:00
|
|
|
import { registerPlugin, Capacitor, WebPlugin } from '@capacitor/core';
|
|
|
|
|
2022-04-04 19:08:27 -05:00
|
|
|
class AbsDatabaseWeb extends WebPlugin {
|
2022-04-03 19:16:17 -05:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:08:27 -05:00
|
|
|
async getDeviceData() {
|
2022-04-03 19:16:17 -05:00
|
|
|
var dd = localStorage.getItem('device')
|
|
|
|
if (dd) {
|
|
|
|
return JSON.parse(dd)
|
|
|
|
}
|
|
|
|
const deviceData = {
|
|
|
|
serverConnectionConfigs: [],
|
|
|
|
lastServerConnectionConfigId: null,
|
|
|
|
localLibraryItemIdPlaying: null
|
|
|
|
}
|
|
|
|
return deviceData
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:08:27 -05:00
|
|
|
async setCurrentServerConnectionConfig(serverConnectionConfig) {
|
|
|
|
var deviceData = await this.getDeviceData()
|
2022-04-03 19:16:17 -05:00
|
|
|
|
|
|
|
var ssc = deviceData.serverConnectionConfigs.find(_ssc => _ssc.id == serverConnectionConfig.id)
|
|
|
|
if (ssc) {
|
|
|
|
deviceData.lastServerConnectionConfigId = ssc.id
|
|
|
|
ssc.name = `${ssc.address} (${serverConnectionConfig.username})`
|
|
|
|
ssc.token = serverConnectionConfig.token
|
|
|
|
ssc.username = serverConnectionConfig.username
|
|
|
|
localStorage.setItem('device', JSON.stringify(deviceData))
|
|
|
|
} else {
|
|
|
|
ssc = {
|
|
|
|
id: encodeURIComponent(Buffer.from(`${serverConnectionConfig.address}@${serverConnectionConfig.username}`).toString('base64')),
|
|
|
|
index: deviceData.serverConnectionConfigs.length,
|
|
|
|
name: `${serverConnectionConfig.address} (${serverConnectionConfig.username})`,
|
|
|
|
username: serverConnectionConfig.username,
|
|
|
|
address: serverConnectionConfig.address,
|
|
|
|
token: serverConnectionConfig.token
|
|
|
|
}
|
|
|
|
deviceData.serverConnectionConfigs.push(ssc)
|
|
|
|
deviceData.lastServerConnectionConfigId = ssc.id
|
|
|
|
localStorage.setItem('device', JSON.stringify(deviceData))
|
|
|
|
}
|
|
|
|
return ssc
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:08:27 -05:00
|
|
|
async removeServerConnectionConfig(serverConnectionConfigCallObject) {
|
2022-04-03 19:16:17 -05:00
|
|
|
var serverConnectionConfigId = serverConnectionConfigCallObject.serverConnectionConfigId
|
2022-04-04 19:08:27 -05:00
|
|
|
var deviceData = await this.getDeviceData()
|
2022-04-03 19:16:17 -05:00
|
|
|
deviceData.serverConnectionConfigs = deviceData.serverConnectionConfigs.filter(ssc => ssc.id == serverConnectionConfigId)
|
|
|
|
localStorage.setItem('device', JSON.stringify(deviceData))
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:08:27 -05:00
|
|
|
async logout() {
|
|
|
|
var deviceData = await this.getDeviceData()
|
|
|
|
deviceData.lastServerConnectionConfigId = null
|
|
|
|
localStorage.setItem('device', JSON.stringify(deviceData))
|
2022-04-03 19:16:17 -05:00
|
|
|
}
|
2022-04-08 18:07:31 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// For testing on web
|
|
|
|
//
|
|
|
|
async getLocalFolders() {
|
|
|
|
return {
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
id: 'test1',
|
|
|
|
name: 'Audiobooks',
|
|
|
|
contentUrl: 'test',
|
|
|
|
absolutePath: '/audiobooks',
|
|
|
|
simplePath: 'audiobooks',
|
|
|
|
storageType: 'primary',
|
|
|
|
mediaType: 'book'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async getLocalFolder({ folderId }) {
|
|
|
|
return this.getLocalFolders().then((data) => data.folders[0])
|
|
|
|
}
|
|
|
|
async getLocalLibraryItems(payload) {
|
|
|
|
return {
|
|
|
|
localLibraryItems: [{
|
|
|
|
id: 'local_test',
|
|
|
|
libraryItemId: 'test34',
|
|
|
|
folderId: 'test1',
|
|
|
|
absolutePath: 'a',
|
|
|
|
contentUrl: 'c',
|
|
|
|
isInvalid: false,
|
|
|
|
mediaType: 'book',
|
|
|
|
media: {
|
|
|
|
metadata: {
|
|
|
|
title: 'Test Book',
|
|
|
|
authorName: 'Test Author Name'
|
|
|
|
},
|
|
|
|
coverPath: null,
|
|
|
|
tags: [],
|
|
|
|
audioFiles: [],
|
|
|
|
chapters: [],
|
|
|
|
tracks: [
|
|
|
|
{
|
|
|
|
index: 1,
|
|
|
|
startOffset: 0,
|
|
|
|
duration: 10000,
|
|
|
|
title: 'Track Title 1',
|
|
|
|
contentUrl: 'test',
|
|
|
|
mimeType: 'audio/mpeg',
|
|
|
|
metadata: null,
|
|
|
|
isLocal: true,
|
|
|
|
localFileId: 'lf1',
|
|
|
|
audioProbeResult: {}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
localFiles: [
|
|
|
|
{
|
|
|
|
id: 'lf1',
|
|
|
|
filename: 'lf1.mp3',
|
|
|
|
contentUrl: 'test',
|
|
|
|
absolutePath: 'test',
|
|
|
|
simplePath: 'test',
|
|
|
|
mimeType: 'audio/mpeg',
|
|
|
|
size: 39048290
|
|
|
|
}
|
|
|
|
],
|
|
|
|
coverContentUrl: null,
|
|
|
|
coverAbsolutePath: null,
|
|
|
|
isLocal: true
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async getLocalLibraryItemsInFolder({ folderId }) {
|
|
|
|
return this.getLocalLibraryItems()
|
|
|
|
}
|
|
|
|
async getLocalLibraryItem({ id }) {
|
|
|
|
return this.getLocalLibraryItems().then((data) => data.localLibraryItems[0])
|
|
|
|
}
|
|
|
|
async getLocalLibraryItemByLLId({ libraryItemId }) {
|
|
|
|
return this.getLocalLibraryItems().then((data) => data.localLibraryItems.find(lli => lli.libraryItemId == libraryItemId))
|
|
|
|
}
|
2022-04-03 19:16:17 -05:00
|
|
|
}
|
|
|
|
|
2022-04-04 19:08:27 -05:00
|
|
|
const AbsDatabase = registerPlugin('AbsDatabase', {
|
|
|
|
web: () => new AbsDatabaseWeb()
|
2022-04-03 19:16:17 -05:00
|
|
|
})
|
|
|
|
|
2022-04-04 19:08:27 -05:00
|
|
|
export { AbsDatabase }
|