Skip to content

Commit

Permalink
UBERF-7166 Use blob lookup for direct collaborative doc access (#5774)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Onnikov <[email protected]>
  • Loading branch information
aonnikov committed Jun 11, 2024
1 parent 673fc99 commit f8b7922
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import { textEditorCommandHandler } from '../commands'
import { EditorKit } from '../kits/editor-kit'
import textEditorPlugin from '../plugin'
import { MinioProvider } from '../provider/minio'
import { DirectStorageProvider } from '../provider/storage'
import { TiptapCollabProvider } from '../provider/tiptap'
import { formatCollaborativeDocumentId, formatPlatformDocumentId } from '../provider/utils'
import {
Expand Down Expand Up @@ -135,7 +135,7 @@
const ydoc = getContext<YDoc>(CollaborationIds.Doc) ?? new YDoc()
const contextProvider = getContext<TiptapCollabProvider>(CollaborationIds.Provider)
const localProvider = contextProvider === undefined ? new MinioProvider(documentId, ydoc) : undefined
const localProvider = contextProvider === undefined ? new DirectStorageProvider(collaborativeDoc, ydoc) : undefined
const remoteProvider: TiptapCollabProvider =
contextProvider ??
Expand Down
68 changes: 0 additions & 68 deletions packages/text-editor/src/provider/minio.ts

This file was deleted.

71 changes: 71 additions & 0 deletions packages/text-editor/src/provider/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Copyright © 2023 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.
//
import { type Blob, type CollaborativeDoc, type Ref, collaborativeDocParse } from '@hcengineering/core'
import { getBlobHref } from '@hcengineering/presentation'
import { ObservableV2 as Observable } from 'lib0/observable'
import { applyUpdate, type Doc as YDoc } from 'yjs'

interface EVENTS {
synced: (...args: any[]) => void
}

async function fetchContent (blob: Ref<Blob>, doc: YDoc): Promise<boolean> {
const update = await fetchBlobContent(blob)
if (update !== undefined) {
applyUpdate(doc, update)
return true
}

return false
}

async function fetchBlobContent (_id: Ref<Blob>): Promise<Uint8Array | undefined> {
try {
const href = await getBlobHref(undefined, _id)
const res = await fetch(href)

if (res.ok) {
const blob = await res.blob()
const buffer = await blob.arrayBuffer()
return new Uint8Array(buffer)
}
} catch (err: any) {
console.error(err)
}

return undefined
}

export class DirectStorageProvider extends Observable<EVENTS> {
loaded: Promise<void>

constructor (collaborativeDoc: CollaborativeDoc, doc: YDoc) {
super()

this.loaded = new Promise((resolve) => {
this.on('synced', resolve)
})

const { documentId, versionId } = collaborativeDocParse(collaborativeDoc)

if (versionId === 'HEAD') {
void fetchContent(documentId as Ref<Blob>, doc).then((synced) => {
if (synced) {
this.emit('synced', [this])
}
})
}
}
}

0 comments on commit f8b7922

Please sign in to comment.