-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.js
148 lines (115 loc) · 4.14 KB
/
dataset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
const { Entry } = require('./entry');
const Visibility = require('./visibility');
module.exports = class Dataset {
constructor(registry, org, ds) {
this.registry = registry;
this.org = org;
this.ds = ds;
}
remoteUri(path) {
const { remote, secure } = this.registry;
const proto = secure ? "ddb" : "ddb+unsafe";
const p = (path && path !== ".") ? `/${path}` : "";
return `${proto}://${remote}/${this.org}/${this.ds}${p}`;
}
get baseApi() {
return `/orgs/${this.org}/ds/${this.ds}`;
}
downloadUrl(paths, options = {}) {
if (typeof paths === "string") paths = [paths];
let url = `${this.baseApi}/download`;
let q = {};
if (paths !== undefined) {
if (paths.length > 1) q.path = paths.join(",");
else url += `/${paths[0]}`;
}
if (options.inline) q.inline = "1";
q = new URLSearchParams(q).toString();
if (q) url += `?${q}`;
return url;
}
thumbUrl(path, size) {
let url = `${this.baseApi}/thumb?path=${encodeURIComponent(path)}`;
if (size) url += `&size=${size}`;
return url;
}
tileUrl(path, tz, tx, ty, options = {}) {
let retina = "";
if (options.retina) retina = "@2x";
let url = `${this.baseApi}/tiles/${tz}/${tx}/${ty}${retina}.png?path=${path}`;
return url;
}
Entry(fileEntry) {
return new Entry(this, fileEntry);
}
async download(paths) {
return this.registry.postRequest(`${this.baseApi}/download`, { path: paths });
}
async getFileContents(path) {
const url = this.downloadUrl(path, { inline: true });
return this.registry.getRequest(url);
}
async info() {
return this.registry.getRequest(`${this.baseApi}`);
}
async update(name, visibility) {
return this.registry.putRequest(`${this.baseApi}`, {
name, visibility
});
}
async list(path) {
return this.registry.postRequest(`${this.baseApi}/list`, { path });
}
async listOne(path){
const l = await this.list(path);
if (l.length === 1){
return l[0];
}else if (l.length === 0){
throw new Error(`Cannot find: ${path}. It might have been renamed or moved.`);
}else{
throw new Error("listOne returned more than 1 element");
}
}
async search(query){
return this.registry.postRequest(`${this.baseApi}/search`, { query });
}
async delete() {
return this.registry.deleteRequest(`${this.baseApi}`);
}
async deleteObj(path) {
return this.registry.deleteRequest(`${this.baseApi}/obj`, { path });
}
async moveObj(source, dest) {
return this.registry.putRequest(`${this.baseApi}/obj`, { source, dest });
}
async writeObj(path, content) {
return this.registry.postRequest(`${this.baseApi}/obj`, { path, file: new Blob([content]) });
}
async createFolder(path) {
return this.registry.postRequest(`${this.baseApi}/obj`, { path });
}
async rename(slug) {
if (typeof slug !== "string") throw new Error(`Invalid slug ${slug}`);
return this.registry.postRequest(`${this.baseApi}/rename`, { slug });
}
async metaSet(key, data, path = "") {
if (!key) throw new Error(`Invalid key ${key}`);
if (data === undefined) throw new Error(`Invalid data`);
if (typeof data === "string") data = JSON.stringify(data);
return this.registry.postRequest(`${this.baseApi}/meta/set`, {
key, data, path
});
}
async setVisibility(visibility) {
const v = parseInt(visibility);
if (isNaN(v)) throw Error("Invalid visibility value");
return this.metaSet("visibility", v);
}
async getVisibility(){
const info = await this.info();
return info[0].properties?.meta?.visibility?.data;
}
};