/
ContentLibrary.ts
235 lines (203 loc) · 6.56 KB
/
ContentLibrary.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// import { getMime } from "name2mime";
import { QlikRepositoryClient, QlikGenericRestClient } from "qlik-rest-api";
import {
IStaticContentReferenceCondensed,
IUpdateObjectOptions,
} from "./types/interfaces";
import { IHttpStatus } from "./types/ranges";
import {
IContentLibrary,
IContentLibraryFile,
IContentLibraryUpdate,
IContentLibraryImport,
} from "./types/interfaces";
import { UpdateCommonProperties } from "./util/UpdateCommonProps";
import { URLBuild } from "./util/generic";
import { IncomingMessage } from "http";
export interface IClassContentLibrary {
export(arg: { sourceFileName?: string }): Promise<IContentLibraryFile>;
exportMany(arg?: {
sourceFileNames: string[];
}): Promise<IContentLibraryFile[]>;
remove(): Promise<IHttpStatus>;
importFile(arg: IContentLibraryImport): Promise<{
status: IHttpStatus;
fileDetails: IStaticContentReferenceCondensed;
}>;
importFileMany(
arg: IContentLibraryImport[]
): Promise<
{ status: IHttpStatus; fileDetails: IStaticContentReferenceCondensed }[]
>;
removeFile(arg: { externalPath: string }): Promise<IHttpStatus>;
removeFileMany(
arg: string[]
): Promise<{ status: number; externalPath: string }[]>;
update(
arg: IContentLibraryUpdate,
options?: IUpdateObjectOptions
): Promise<IContentLibrary>;
details: IContentLibrary;
}
export class ContentLibrary implements IClassContentLibrary {
#id: string;
#repoClient: QlikRepositoryClient;
#genericClient: QlikGenericRestClient;
details: IContentLibrary;
constructor(
repoClient: QlikRepositoryClient,
id: string,
details?: IContentLibrary,
genericClient?: QlikGenericRestClient
) {
if (!id) throw new Error(`contentLibrary.get: "id" parameter is required`);
this.#id = id;
this.#repoClient = repoClient;
this.#genericClient = genericClient;
if (details) this.details = details;
}
async init() {
if (!this.details) {
this.details = await this.#repoClient
.Get<IContentLibrary>(`contentlibrary/${this.#id}`)
.then((res) => res.data);
}
}
async export(arg: { sourceFileName: string }) {
if (!arg.sourceFileName)
throw new Error(
`contentLibrary.export: "sourceFileName" parameter is required`
);
if (
this.#repoClient.configFull.port &&
this.#repoClient.configFull.port == 4242
)
throw new Error(
`contentLibrary.export: exporting content library is not possible when the authentication is made with certificates`
);
const file = this.details.references.filter((r) => {
return r.logicalPath.replace(/^.*[\\\/]/, "") == arg.sourceFileName;
});
if (file.length == 0)
throw new Error(
`contentLibrary.export: file "${arg.sourceFileName}" not found in content library "${this.details.name}"`
);
const logicalPath =
file[0].logicalPath[0] == "/"
? file[0].logicalPath.substring(1)
: file[0].logicalPath;
const fileContent = await this.#genericClient.Get<IncomingMessage>(
logicalPath,
"",
"stream"
);
return {
name: file[0].logicalPath.replace(/^.*[\\\/]/, ""),
path: `/${file[0].logicalPath.split("/").slice(2).join("/")}`,
file: fileContent.data,
};
}
async exportMany(arg?: { sourceFileNames: string[] }) {
let files: IStaticContentReferenceCondensed[] = [];
if (!arg) files = this.details.references;
if (arg && !arg.sourceFileNames)
throw new Error(
`contentLibrary.exportMany: "sourceFileNames" parameter is required`
);
if (arg)
files = this.details.references.filter((r) => {
return arg.sourceFileNames.includes(
r.logicalPath.replace(`/content/${this.details.name}/`, "")
);
});
return Promise.all(
files.map((r) => {
return this.export({
sourceFileName: r.logicalPath.replace(/^.*[\\\/]/, ""),
});
})
);
}
public async importFile(arg: IContentLibraryImport) {
if (!arg.file)
throw new Error(`contentLibrary.import: "file" parameter is required`);
if (!arg.externalPath)
throw new Error(
`contentLibrary.import: "externalPath" parameter is required`
);
const urlBuild = new URLBuild(
`contentlibrary/${this.details.name}/uploadfile`
);
urlBuild.addParam("externalpath", arg.externalPath);
urlBuild.addParam("overwrite", arg.overwrite || undefined);
// const mimeType = getMime(arg.file);
return await this.#repoClient
.Post<{
status: IHttpStatus;
fileDetails: IStaticContentReferenceCondensed;
}>(urlBuild.getUrl(), arg.file)
.then(async (res) => {
this.details = await this.#repoClient
.Get<IContentLibrary>(`contentlibrary/${this.#id}`)
.then((res) => res.data);
const toReplace = `/content/${this.details.name}/`;
const fileDetails = this.details.references.filter(
(r) => r.externalPath.replace(toReplace, "") == arg.externalPath
)[0];
return { status: res.status, fileDetails: fileDetails };
});
}
public async importFileMany(arg: IContentLibraryImport[]) {
return Promise.all(
arg.map((cl) => {
return this.importFile(cl);
})
);
}
public async removeFile(arg: { externalPath: string }) {
if (!arg.externalPath)
throw new Error(
`contentLibrary.removeFile: "externalPath" parameter is required`
);
const urlBuild = new URLBuild(
`contentlibrary/${this.details.name}/deletecontent`
);
urlBuild.addParam("externalpath", arg.externalPath);
return await this.#repoClient
.Delete(urlBuild.getUrl())
.then((res) => res.status);
}
public async removeFileMany(arg: string[]) {
return await Promise.all(
arg.map((fileName) => {
return this.removeFile({ externalPath: fileName }).then((r) => ({
status: r,
externalPath: fileName,
}));
})
);
}
public async remove() {
return await this.#repoClient
.Delete(`contentlibrary/${this.details.id}`)
.then((res) => res.status);
}
public async update(
arg: IContentLibraryUpdate,
options?: IUpdateObjectOptions
) {
let updateCommon = new UpdateCommonProperties(
this.#repoClient,
this.details,
arg,
options
);
if (arg.name) this.details.name = arg.name;
this.details = await updateCommon.updateAll();
return await this.#repoClient
.Put<IContentLibrary>(`contentlibrary/${this.details.id}`, {
...this.details,
})
.then((res) => res.data);
}
}