Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate icon plugin into MediaServer #369

Merged
merged 6 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/blob-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ import b4a from 'b4a'
export class BlobApi {
#blobStore
#getMediaBaseUrl
#projectPublicId

/**
* @param {object} options
* @param {string} options.projectPublicId
* @param {import('./blob-store/index.js').BlobStore} options.blobStore
* @param {() => Promise<string>} options.getMediaBaseUrl
*/
constructor({ projectPublicId, blobStore, getMediaBaseUrl }) {
constructor({ blobStore, getMediaBaseUrl }) {
this.#blobStore = blobStore
this.#getMediaBaseUrl = getMediaBaseUrl
this.#projectPublicId = projectPublicId
}

/**
Expand All @@ -38,9 +35,7 @@ export class BlobApi {
base += '/'
}

return (
base + `${this.#projectPublicId}/${driveId}/${type}/${variant}/${name}`
)
return base + `${driveId}/${type}/${variant}/${name}`
}

/**
Expand Down
11 changes: 3 additions & 8 deletions src/icon-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const MIME_TO_EXTENSION = {
}

export class IconApi {
#projectId
#dataType
#dataStore
#getMediaBaseUrl
Expand All @@ -36,13 +35,11 @@ export class IconApi {
* import('@mapeo/schema').IconValue
* >} opts.iconDataType
* @param {import('./datastore/index.js').DataStore<'config'>} opts.iconDataStore
* @param {string} opts.projectId
* @param {() => Promise<string>} opts.getMediaBaseUrl
*/
constructor({ iconDataType, iconDataStore, projectId, getMediaBaseUrl }) {
constructor({ iconDataType, iconDataStore, getMediaBaseUrl }) {
this.#dataType = iconDataType
this.#dataStore = iconDataStore
this.#projectId = projectId
this.#getMediaBaseUrl = getMediaBaseUrl
}

Expand Down Expand Up @@ -110,15 +107,13 @@ export class IconApi {
base += '/'
}

base += `${this.#projectId}/${iconId}/`

const mimeExtension = MIME_TO_EXTENSION[opts.mimeType]

if (opts.mimeType === 'image/svg+xml') {
return base + `${opts.size}${mimeExtension}`
return base + `${iconId}/${opts.size}${mimeExtension}`
}

return base + `${opts.size}@${opts.pixelDensity}x${mimeExtension}`
return base + `${iconId}/${opts.size}@${opts.pixelDensity}x${mimeExtension}`
}
}

Expand Down
38 changes: 23 additions & 15 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export class MapeoProject {
#capabilities
#ownershipWriteDone
#memberApi
#projectPublicId
#iconApi
#syncApi
#l
Expand Down Expand Up @@ -98,7 +97,6 @@ export class MapeoProject {
this.#l = Logger.create('project', logger)
this.#deviceId = getDeviceId(keyManager)
this.#projectId = projectKeyToId(projectKey)
this.#projectPublicId = projectKeyToPublicId(projectKey)

///////// 1. Setup database
const sqlite = new Database(dbPath)
Expand Down Expand Up @@ -137,6 +135,7 @@ export class MapeoProject {
coreOwnershipTable,
roleTable,
deviceInfoTable,
iconTable,
],
sqlite,
getWinner,
Expand Down Expand Up @@ -219,16 +218,6 @@ export class MapeoProject {
}),
}

this.#blobStore = new BlobStore({
coreManager: this.#coreManager,
})

this.$blobs = new BlobApi({
projectPublicId: this.#projectPublicId,
blobStore: this.#blobStore,
getMediaBaseUrl: async () => getMediaBaseUrl('blobs'),
})

this.#coreOwnership = new CoreOwnership({
dataType: this.#dataTypes.coreOwnership,
})
Expand All @@ -254,13 +243,32 @@ export class MapeoProject {
},
})

const projectPublicId = projectKeyToPublicId(projectKey)

this.#blobStore = new BlobStore({
coreManager: this.#coreManager,
})

this.$blobs = new BlobApi({
blobStore: this.#blobStore,
getMediaBaseUrl: async () => {
let base = await getMediaBaseUrl('blobs')
if (!base.endsWith('/')) {
base += '/'
}
return base + projectPublicId
},
})

this.#iconApi = new IconApi({
iconDataStore: this.#dataStores.config,
iconDataType: this.#dataTypes.icon,
projectId: this.#projectId,
// TODO: Update after merging https://github.com/digidem/mapeo-core-next/pull/365
getMediaBaseUrl: async () => {
throw new Error('Not yet implemented')
let base = await getMediaBaseUrl('icons')
if (!base.endsWith('/')) {
base += '/'
}
return base + projectPublicId
},
})

Expand Down
7 changes: 7 additions & 0 deletions src/media-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import pTimeout from 'p-timeout'
import StateMachine from 'start-stop-state-machine'

import BlobServerPlugin from './fastify-plugins/blobs.js'
import IconServerPlugin from './fastify-plugins/icons.js'

import { kBlobStore } from './mapeo-project.js'

export const BLOBS_PREFIX = 'blobs'
Expand Down Expand Up @@ -44,6 +46,11 @@ export class MediaServer {
},
})

this.#fastify.register(IconServerPlugin, {
prefix: ICONS_PREFIX,
getProject,
})

this.#serverState = new StateMachine({
start: this.#startServer.bind(this),
stop: this.#stopServer.bind(this),
Expand Down
Loading
Loading