2024-05-15 05:50:10 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2022 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2024-05-30 09:19:23 +00:00
|
|
|
import core, {
|
2024-05-15 05:50:10 +00:00
|
|
|
Class,
|
|
|
|
Doc,
|
|
|
|
DocumentQuery,
|
|
|
|
DocumentUpdate,
|
|
|
|
Domain,
|
|
|
|
FindOptions,
|
|
|
|
FindResult,
|
|
|
|
Hierarchy,
|
|
|
|
IndexingConfiguration,
|
|
|
|
MeasureContext,
|
|
|
|
ModelDb,
|
|
|
|
Ref,
|
|
|
|
StorageIterator,
|
|
|
|
Tx,
|
|
|
|
TxResult,
|
|
|
|
WorkspaceId,
|
|
|
|
type Blob
|
|
|
|
} from '@hcengineering/core'
|
|
|
|
import { createMongoAdapter } from '@hcengineering/mongo'
|
|
|
|
import { PlatformError, unknownError } from '@hcengineering/platform'
|
2024-05-30 09:19:23 +00:00
|
|
|
import { DbAdapter, StorageAdapter, type StorageAdapterEx } from '@hcengineering/server-core'
|
2024-05-15 05:50:10 +00:00
|
|
|
|
|
|
|
class StorageBlobAdapter implements DbAdapter {
|
|
|
|
constructor (
|
|
|
|
readonly workspaceId: WorkspaceId,
|
|
|
|
readonly client: StorageAdapter, // Should not be closed
|
|
|
|
readonly ctx: MeasureContext,
|
|
|
|
readonly blobAdapter: DbAdapter // A real blob adapter for Blob documents.
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async findAll<T extends Doc>(
|
|
|
|
ctx: MeasureContext,
|
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
query: DocumentQuery<T>,
|
|
|
|
options?: FindOptions<T>
|
|
|
|
): Promise<FindResult<T>> {
|
|
|
|
return await this.blobAdapter.findAll(ctx, _class, query, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
async tx (ctx: MeasureContext, ...tx: Tx[]): Promise<TxResult[]> {
|
|
|
|
throw new PlatformError(unknownError('Direct Blob operations are not possible'))
|
|
|
|
}
|
|
|
|
|
|
|
|
async createIndexes (domain: Domain, config: Pick<IndexingConfiguration<Doc>, 'indexes'>): Promise<void> {}
|
|
|
|
async removeOldIndex (domain: Domain, deletePattern: RegExp, keepPattern: RegExp): Promise<void> {}
|
|
|
|
|
|
|
|
async close (): Promise<void> {
|
|
|
|
await this.blobAdapter.close()
|
|
|
|
}
|
|
|
|
|
2024-06-04 08:58:34 +00:00
|
|
|
find (ctx: MeasureContext, domain: Domain, recheck?: boolean): StorageIterator {
|
|
|
|
return this.blobAdapter.find(ctx, domain, recheck)
|
2024-05-15 05:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async load (ctx: MeasureContext, domain: Domain, docs: Ref<Doc>[]): Promise<Doc[]> {
|
|
|
|
return await this.blobAdapter.load(ctx, domain, docs)
|
|
|
|
}
|
|
|
|
|
|
|
|
async upload (ctx: MeasureContext, domain: Domain, docs: Doc[]): Promise<void> {
|
2024-05-30 09:19:23 +00:00
|
|
|
// We need to update docs to have provider === defualt one.
|
|
|
|
if ('adapters' in this.client) {
|
2024-06-25 06:11:21 +00:00
|
|
|
const toUpload: Doc[] = []
|
2024-05-30 09:19:23 +00:00
|
|
|
const adapterEx = this.client as StorageAdapterEx
|
|
|
|
for (const d of docs) {
|
2024-06-25 06:11:21 +00:00
|
|
|
// We need sync stats to be sure all info are correct from storage.
|
2024-05-30 09:19:23 +00:00
|
|
|
if (d._class === core.class.Blob) {
|
2024-06-25 06:11:21 +00:00
|
|
|
const blob = d as Blob
|
|
|
|
const blobStat = await this.client.stat(ctx, this.workspaceId, blob.storageId)
|
|
|
|
if (blobStat !== undefined) {
|
|
|
|
blob.provider = adapterEx.defaultAdapter
|
|
|
|
blob.etag = blobStat.etag
|
|
|
|
blob.contentType = blobStat.contentType
|
|
|
|
blob.version = blobStat.version
|
|
|
|
blob.size = blobStat.size
|
|
|
|
|
|
|
|
toUpload.push(blob)
|
|
|
|
}
|
2024-05-30 09:19:23 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-25 06:11:21 +00:00
|
|
|
docs = toUpload
|
2024-05-30 09:19:23 +00:00
|
|
|
}
|
2024-05-15 05:50:10 +00:00
|
|
|
await this.blobAdapter.upload(ctx, domain, docs)
|
|
|
|
}
|
|
|
|
|
|
|
|
async clean (ctx: MeasureContext, domain: Domain, docs: Ref<Doc>[]): Promise<void> {
|
|
|
|
await Promise.all([this.blobAdapter.clean(ctx, domain, docs), this.client.remove(this.ctx, this.workspaceId, docs)])
|
|
|
|
}
|
|
|
|
|
|
|
|
async update (ctx: MeasureContext, domain: Domain, operations: Map<Ref<Doc>, DocumentUpdate<Doc>>): Promise<void> {
|
|
|
|
await this.blobAdapter.update(ctx, domain, operations)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export async function createStorageDataAdapter (
|
|
|
|
ctx: MeasureContext,
|
|
|
|
hierarchy: Hierarchy,
|
|
|
|
url: string,
|
|
|
|
workspaceId: WorkspaceId,
|
|
|
|
modelDb: ModelDb,
|
2024-05-27 07:24:47 +00:00
|
|
|
storage: StorageAdapter
|
2024-05-15 05:50:10 +00:00
|
|
|
): Promise<DbAdapter> {
|
|
|
|
if (storage === undefined) {
|
|
|
|
throw new Error('minio storage adapter require minio')
|
|
|
|
}
|
|
|
|
// We need to create bucket if it doesn't exist
|
2024-05-27 07:24:47 +00:00
|
|
|
await storage.make(ctx, workspaceId)
|
2024-06-04 08:58:34 +00:00
|
|
|
|
|
|
|
const storageEx = 'adapters' in storage ? (storage as StorageAdapterEx) : undefined
|
|
|
|
|
2024-05-15 05:50:10 +00:00
|
|
|
const blobAdapter = await createMongoAdapter(ctx, hierarchy, url, workspaceId, modelDb, undefined, {
|
|
|
|
calculateHash: (d) => {
|
2024-06-04 08:58:34 +00:00
|
|
|
const blob = d as Blob
|
|
|
|
if (storageEx?.adapters !== undefined && storageEx.adapters.get(blob.provider) === undefined) {
|
|
|
|
return blob.etag + '_' + storageEx.defaultAdapter // Replace tag to be able to move to new provider
|
|
|
|
}
|
|
|
|
return blob.etag
|
2024-05-15 05:50:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return new StorageBlobAdapter(workspaceId, storage, ctx, blobAdapter)
|
|
|
|
}
|