Skip to content

Commit 0cb3f04

Browse files
committed
support blobs - uuids
Signed-off-by: Anna Khismatullina <[email protected]>
1 parent 1e0e6bf commit 0cb3f04

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

dev/import-tool/docs/huly/example-workspace/SlaveCard/Frodo.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ color: pink
99
x: from familiar
1010
y: from minion
1111
multy-enum: [Beta]
12-
# attachments:
13-
# - ../../CARDS_INSTRUCTIONS.md
12+
attachments:
13+
- ../../CARDS_INSTRUCTIONS.md
1414
blobs:
1515
- ../files/screenshot.png
16-
# - ../README.md
16+
- ../../CARDS_INSTRUCTIONS.md
1717
---
1818

1919
A brave hobbit guided by Gandalf.

packages/importer/src/huly/metadata.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Tag } from '@hcengineering/card'
2-
import { Association, Attribute, Doc, generateId, Ref, Blob as PlatformBlob } from '@hcengineering/core'
3-
import path from 'path'
4-
import { UnifiedDoc, UnifiedFile } from '../types'
2+
import { Association, Attribute, Doc, generateId, generateUuid, Blob as PlatformBlob, Ref } from '@hcengineering/core'
3+
import { UnifiedDoc } from '../types'
54

65
export interface RelationMetadata { // todo: rename
76
association: Ref<Association>
@@ -20,15 +19,24 @@ export interface TagMetadata {
2019
export class MetadataStorage {
2120
private readonly pathToRef = new Map<string, Ref<Doc>>() // todo: attachments to a separate map?
2221
private readonly pathToMetadata = new Map<string, TagMetadata>()
23-
private readonly pathToBlobUuid = new Map<string, Ref<PlatformBlob>>() // todo: blobs to a separate map?
22+
private readonly pathToBlobUuid = new Map<string, Ref<PlatformBlob>>()
2423

25-
public getIdByAbsolutePath (path: string): Ref<Doc> {
26-
let id = this.pathToRef.get(path)
27-
if (id === undefined) {
28-
id = generateId()
29-
this.pathToRef.set(path, id)
24+
public getRefByPath (path: string): Ref<Doc> {
25+
let ref = this.pathToRef.get(path)
26+
if (ref === undefined) {
27+
ref = generateId()
28+
this.pathToRef.set(path, ref)
3029
}
31-
return id
30+
return ref
31+
}
32+
33+
public getUuidByPath (path: string): Ref<PlatformBlob> {
34+
let uuid = this.pathToBlobUuid.get(path)
35+
if (uuid === undefined) {
36+
uuid = generateUuid() as Ref<PlatformBlob>
37+
this.pathToBlobUuid.set(path, uuid)
38+
}
39+
return uuid
3240
}
3341

3442
public hasMetadata (path: string): boolean {
@@ -45,7 +53,7 @@ export class MetadataStorage {
4553

4654
public setAttributes (path: string, attributes: MapAttributeToUnifiedDoc): void {
4755
const metadata = this.pathToMetadata.get(path) ?? {
48-
_id: this.getIdByAbsolutePath(path),
56+
_id: this.getRefByPath(path),
4957
attributes: new Map(),
5058
associations: new Map()
5159
}
@@ -55,7 +63,7 @@ export class MetadataStorage {
5563

5664
public addAssociation (tagPath: string, propName: string, relationMetadata: RelationMetadata): void {
5765
const metadata = this.pathToMetadata.get(tagPath) ?? {
58-
_id: this.getIdByAbsolutePath(tagPath),
66+
_id: this.getRefByPath(tagPath),
5967
attributes: new Map(),
6068
associations: new Map()
6169
}

packages/importer/src/huly/unified.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import core, {
99
Doc,
1010
Enum,
1111
generateId,
12-
Blob as PlatformBlob,
1312
Ref,
1413
Relation
1514
} from '@hcengineering/core'
@@ -62,7 +61,7 @@ export class UnifiedDocProcessor {
6261

6362
switch (yamlConfig?.class) {
6463
case card.class.MasterTag: {
65-
const masterTagId = this.metadataStorage.getIdByAbsolutePath(yamlPath) as Ref<MasterTag>
64+
const masterTagId = this.metadataStorage.getRefByPath(yamlPath) as Ref<MasterTag>
6665
const masterTag = await this.createMasterTag(yamlConfig, masterTagId, parentMasterTagId)
6766
const masterTagAttrs = await this.createAttributes(yamlPath, yamlConfig, masterTagId)
6867

@@ -113,7 +112,7 @@ export class UnifiedDocProcessor {
113112
if (fs.existsSync(yamlPath)) {
114113
const yamlConfig = yaml.load(fs.readFileSync(yamlPath, 'utf8')) as Record<string, any>
115114
if (yamlConfig?.class === card.class.MasterTag) {
116-
masterTagId = this.metadataStorage.getIdByAbsolutePath(yamlPath) as Ref<MasterTag>
115+
masterTagId = this.metadataStorage.getRefByPath(yamlPath) as Ref<MasterTag>
117116
this.metadataStorage.getAssociations(yamlPath).forEach((relationMetadata, propName) => {
118117
masterTagRelations.set(propName, relationMetadata)
119118
})
@@ -246,7 +245,7 @@ export class UnifiedDocProcessor {
246245
masterTagId: Ref<MasterTag>,
247246
parentTagId?: Ref<Tag>
248247
): Promise<void> {
249-
const tagId = this.metadataStorage.getIdByAbsolutePath(tagPath) as Ref<Tag>
248+
const tagId = this.metadataStorage.getRefByPath(tagPath) as Ref<Tag>
250249
const tag = await this.createTag(tagConfig, tagId, masterTagId, parentTagId)
251250

252251
const attributes = await this.createAttributes(tagPath, tagConfig, tagId)
@@ -342,7 +341,7 @@ export class UnifiedDocProcessor {
342341
const baseType: Record<string, any> = {}
343342
baseType._class = core.class.RefTo
344343
const refPath = path.resolve(path.dirname(currentPath), property.refTo)
345-
baseType.to = this.metadataStorage.getIdByAbsolutePath(refPath)
344+
baseType.to = this.metadataStorage.getRefByPath(refPath)
346345
baseType.label = core.string.Ref
347346
type = property.isArray === true
348347
? {
@@ -355,7 +354,7 @@ export class UnifiedDocProcessor {
355354
const baseType: Record<string, any> = {}
356355
baseType._class = core.class.EnumOf
357356
const enumPath = path.resolve(path.dirname(currentPath), property.enumOf)
358-
baseType.of = this.metadataStorage.getIdByAbsolutePath(enumPath)
357+
baseType.of = this.metadataStorage.getRefByPath(enumPath)
359358
baseType.label = 'core:string:Enum'
360359
type = property.isArray === true
361360
? {
@@ -398,7 +397,7 @@ export class UnifiedDocProcessor {
398397
const tags = rawTags !== undefined ? (Array.isArray(rawTags) ? rawTags : [rawTags]) : []
399398
const blobs = rawBlobs !== undefined ? (Array.isArray(rawBlobs) ? rawBlobs : [rawBlobs]) : []
400399

401-
const cardId = this.metadataStorage.getIdByAbsolutePath(cardPath) as Ref<Card>
400+
const cardId = this.metadataStorage.getRefByPath(cardPath) as Ref<Card>
402401
const cardProps: Record<string, any> = {
403402
_id: cardId,
404403
space: core.space.Workspace,
@@ -450,7 +449,7 @@ export class UnifiedDocProcessor {
450449
for (const val of values) {
451450
if (attrBaseType._class === core.class.RefTo) {
452451
const refPath = path.resolve(path.dirname(cardPath), val)
453-
const ref = this.metadataStorage.getIdByAbsolutePath(refPath) as Ref<Card>
452+
const ref = this.metadataStorage.getRefByPath(refPath) as Ref<Card>
454453
propValues.push(ref)
455454
} else {
456455
propValues.push(val)
@@ -463,7 +462,7 @@ export class UnifiedDocProcessor {
463462
throw new Error(`Association not found: ${key}, ${cardPath}`) // todo: keep the error till builder validation
464463
}
465464
const otherCardPath = path.resolve(path.dirname(cardPath), value) // todo: value can be array of paths
466-
const otherCardId = this.metadataStorage.getIdByAbsolutePath(otherCardPath) as Ref<Card>
465+
const otherCardId = this.metadataStorage.getRefByPath(otherCardPath) as Ref<Card>
467466
const relation: UnifiedDoc<Relation> = this.createRelation(metadata, cardId, otherCardId)
468467
relations.push(relation)
469468
}
@@ -509,7 +508,7 @@ export class UnifiedDocProcessor {
509508
for (const tagPath of tags) {
510509
const cardDir = path.dirname(cardPath)
511510
const tagAbsPath = path.resolve(cardDir, tagPath)
512-
const tagId = this.metadataStorage.getIdByAbsolutePath(tagAbsPath) as Ref<Tag>
511+
const tagId = this.metadataStorage.getRefByPath(tagAbsPath) as Ref<Tag>
513512

514513
const tagProps: Record<string, any> = {}
515514
this.metadataStorage.getAttributes(tagAbsPath).forEach((attr, label) => {
@@ -545,11 +544,11 @@ export class UnifiedDocProcessor {
545544
const file = await this.createFile(attachmentPath)
546545
result.files.set(attachmentPath, file)
547546

548-
const attachmentId = this.metadataStorage.getIdByAbsolutePath(attachmentPath) as Ref<Attachment>
547+
const attachmentId = this.metadataStorage.getRefByPath(attachmentPath) as Ref<Attachment>
549548
const attachmentDoc: UnifiedDoc<Attachment> = {
550549
_class: 'attachment:class:Attachment' as Ref<Class<Attachment>>,
551550
props: {
552-
_id: attachmentId, // id for attachment doc
551+
_id: attachmentId,
553552
space: core.space.Workspace,
554553
attachedTo: card.props._id as Ref<Card>,
555554
attachedToClass: card._class,
@@ -582,15 +581,15 @@ export class UnifiedDocProcessor {
582581
): Promise<UnifiedFile> {
583582
// const fileAbsPath = path.resolve(path.dirname(currentPath), filePath)
584583
const fileName = path.basename(fileAbsPath)
585-
const fileId = this.metadataStorage.getIdByAbsolutePath(fileAbsPath) as Ref<PlatformBlob>
584+
const fileUuid = this.metadataStorage.getUuidByPath(fileAbsPath)
586585
const type = contentType(fileName)
587586
const size = fs.statSync(fileAbsPath).size
588587

589588
const file: UnifiedFile = {
590-
_id: fileId, // id for datastore
589+
_id: fileUuid, // id for datastore
591590
name: fileName,
592591
type: type !== false ? type : 'application/octet-stream',
593-
size, // todo: make sure this one is needed
592+
size,
594593
blobProvider: async () => {
595594
const data = fs.readFileSync(fileAbsPath)
596595
const props = type !== false ? { type } : undefined
@@ -608,7 +607,7 @@ export class UnifiedDocProcessor {
608607
const { class: _class, typeA, typeB, type, nameA, nameB } = yamlConfig
609608

610609
const currentPath = path.dirname(yamlPath)
611-
const associationId = this.metadataStorage.getIdByAbsolutePath(yamlPath) as Ref<Association>
610+
const associationId = this.metadataStorage.getRefByPath(yamlPath) as Ref<Association>
612611

613612
const typeAPath = path.resolve(currentPath, typeA)
614613
this.metadataStorage.addAssociation(typeAPath, nameB, {
@@ -624,8 +623,8 @@ export class UnifiedDocProcessor {
624623
type
625624
})
626625

627-
const typeAId = this.metadataStorage.getIdByAbsolutePath(typeAPath) as Ref<MasterTag>
628-
const typeBId = this.metadataStorage.getIdByAbsolutePath(typeBPath) as Ref<MasterTag>
626+
const typeAId = this.metadataStorage.getRefByPath(typeAPath) as Ref<MasterTag>
627+
const typeBId = this.metadataStorage.getRefByPath(typeBPath) as Ref<MasterTag>
629628

630629
return {
631630
_class,
@@ -646,7 +645,7 @@ export class UnifiedDocProcessor {
646645
yamlConfig: Record<string, any>
647646
): Promise<UnifiedDoc<Enum>> {
648647
const { title, values } = yamlConfig
649-
const enumId = this.metadataStorage.getIdByAbsolutePath(yamlPath) as Ref<Enum>
648+
const enumId = this.metadataStorage.getRefByPath(yamlPath) as Ref<Enum>
650649
return {
651650
_class: core.class.Enum,
652651
props: {

0 commit comments

Comments
 (0)