@thatopen/services 0.24.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -6797,6 +6797,15 @@ Object.assign(lookup, {
6797
6797
  });
6798
6798
  const DOCUMENT_SCHEMA = "thatopen/document@1";
6799
6799
  const DOCUMENT_FILE = "document.json";
6800
+ const SELF_VIEWING_FORMATS = [
6801
+ "pdf",
6802
+ "txt",
6803
+ "md",
6804
+ "json",
6805
+ "csv",
6806
+ "tsv",
6807
+ "frag"
6808
+ ];
6800
6809
  const versionTag$1 = () => `v-${Date.now()}`;
6801
6810
  function idOf$1(item) {
6802
6811
  return String(item._id ?? item.id ?? "");
@@ -6806,6 +6815,73 @@ class ProjectDocuments {
6806
6815
  this.client = client;
6807
6816
  this.projectId = projectId;
6808
6817
  }
6818
+ /**
6819
+ * Every document of the project: the folders that carry a descriptor.
6820
+ * Plain folders and loose files simply do not appear. One folder listing
6821
+ * for the project plus one file listing and one descriptor download per
6822
+ * folder, fetched in parallel — the storage offers nothing cheaper yet.
6823
+ */
6824
+ async list() {
6825
+ const folders = await this.client.listFolders({
6826
+ projectId: this.projectId
6827
+ });
6828
+ const found = await Promise.all(
6829
+ folders.map(async (folder) => {
6830
+ const folderId = idOf$1(folder);
6831
+ const descriptor = await this.descriptor(folderId);
6832
+ return descriptor ? { folderId, descriptor } : null;
6833
+ })
6834
+ );
6835
+ return found.filter((d) => d !== null);
6836
+ }
6837
+ /**
6838
+ * Converts a loose file into a document: creates the document folder next
6839
+ * to the file, moves the file inside (moving preserves its id), and writes
6840
+ * the descriptor with the file as `native` — and as `views[0]` too when its
6841
+ * format views itself ({@link SELF_VIEWING_FORMATS}; override per call with
6842
+ * `selfView`).
6843
+ *
6844
+ * A file that carries a processed derivative (the old `derivedFileId` link
6845
+ * in its version metadata — an IFC's frag, a LAS's potree) brings it along:
6846
+ * the derivative moves into the same folder and becomes a view, so the
6847
+ * document is born ready to visualize and the link concept retires.
6848
+ */
6849
+ async fromFile(fileId, options) {
6850
+ const item = await this.client.getFile(fileId);
6851
+ const fileName = String(item.name ?? "");
6852
+ const format2 = formatOf(fileName, item.fileExtension);
6853
+ const name = options?.name ?? fileName.replace(/\.[^.]+$/, "") ?? fileName;
6854
+ const folder = await this.client.createFolder(
6855
+ name,
6856
+ item.folderId ? String(item.folderId) : void 0,
6857
+ this.projectId
6858
+ );
6859
+ const folderId = idOf$1(folder);
6860
+ await this.client.updateFile(fileId, { parentFolderId: folderId });
6861
+ const views = [];
6862
+ const derivedId = await this.#derivedFileId(item);
6863
+ if (derivedId) {
6864
+ try {
6865
+ const derived = await this.client.getFile(derivedId);
6866
+ await this.client.updateFile(derivedId, { parentFolderId: folderId });
6867
+ views.push({
6868
+ fileId: derivedId,
6869
+ format: formatOf(String(derived.name ?? ""), derived.fileExtension)
6870
+ });
6871
+ } catch {
6872
+ }
6873
+ }
6874
+ const selfViews = options?.selfView ?? SELF_VIEWING_FORMATS.includes(format2);
6875
+ if (selfViews) views.push({ fileId, format: format2 });
6876
+ const descriptor = {
6877
+ schema: DOCUMENT_SCHEMA,
6878
+ name,
6879
+ native: { fileId, format: format2 },
6880
+ views
6881
+ };
6882
+ await this.setDescriptor(folderId, descriptor);
6883
+ return { folderId, descriptor };
6884
+ }
6809
6885
  /**
6810
6886
  * The document's descriptor, read and validated, or null when the folder
6811
6887
  * has none yet (an old folder, or a plain folder that is not a document).
@@ -6884,6 +6960,28 @@ class ProjectDocuments {
6884
6960
  projectId: this.projectId
6885
6961
  });
6886
6962
  }
6963
+ /**
6964
+ * The legacy `derivedFileId` link, read from the newest version metadata
6965
+ * that carries one. Only `fromFile` looks at it, to migrate the link into
6966
+ * the folder-document shape; nothing writes it any more.
6967
+ */
6968
+ async #derivedFileId(item) {
6969
+ const versions = [...item.versions ?? []].sort(
6970
+ (a, b) => String(b.createdAt ?? "").localeCompare(String(a.createdAt ?? ""))
6971
+ );
6972
+ for (const version2 of versions) {
6973
+ try {
6974
+ const metadata2 = await this.client.getFileVersionMetadata(
6975
+ idOf$1(item),
6976
+ version2.tag
6977
+ );
6978
+ const derived = metadata2?.derivedFileId;
6979
+ if (typeof derived === "string" && derived) return derived;
6980
+ } catch {
6981
+ }
6982
+ }
6983
+ return void 0;
6984
+ }
6887
6985
  async #descriptorFile(folderId) {
6888
6986
  const items2 = await this.client.listFiles({ folderId });
6889
6987
  const found = items2.find(
@@ -6892,6 +6990,12 @@ class ProjectDocuments {
6892
6990
  return found ? { id: idOf$1(found) } : void 0;
6893
6991
  }
6894
6992
  }
6993
+ function formatOf(fileName, extension) {
6994
+ const fromExt = (extension ?? "").replace(/^\./, "").toLowerCase();
6995
+ if (fromExt) return fromExt;
6996
+ const dot = fileName.lastIndexOf(".");
6997
+ return dot > 0 ? fileName.slice(dot + 1).toLowerCase() : "";
6998
+ }
6895
6999
  function normalize$1(parsed) {
6896
7000
  if (!parsed || typeof parsed !== "object") return null;
6897
7001
  const raw = parsed;