1 | import { __decorate } from "tslib";
|
2 | import { body, cancelableScope, CancelAction } from "@pnp/queryable";
|
3 | import { getGUID, isFunc, stringIsNullOrEmpty, isUrlAbsolute, combine, noInherit } from "@pnp/core";
|
4 | import { _SPCollection, spInvokableFactory, SPInstance, deleteableWithETag, deleteable, } from "../spqueryable.js";
|
5 | import { Item } from "../items/index.js";
|
6 | import { odataUrlFrom } from "../utils/odata-url-from.js";
|
7 | import { defaultPath } from "../decorators.js";
|
8 | import { spPost, spGet } from "../operations.js";
|
9 | import { extractWebUrl } from "../utils/extract-web-url.js";
|
10 | import { toResourcePath } from "../utils/to-resource-path.js";
|
11 | import { encodePath } from "../utils/encode-path-str.js";
|
12 | import { ReadableFile } from "./readable-file.js";
|
13 | import "../context-info/index.js";
|
14 | import { BatchNever } from "../batching.js";
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | let _Files = class _Files extends _SPCollection {
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 | getByUrl(name) {
|
26 | if (/%#/.test(name)) {
|
27 | throw Error("For file names containing % or # please use web.getFileByServerRelativePath");
|
28 | }
|
29 | return File(this).concat(`('${encodePath(name)}')`);
|
30 | }
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | async addUsingPath(url, content, parameters = { Overwrite: false }) {
|
39 | const path = [`AddUsingPath(decodedurl='${encodePath(url)}'`];
|
40 | if (parameters) {
|
41 | if (parameters.Overwrite) {
|
42 | path.push(",Overwrite=true");
|
43 | }
|
44 | if (parameters.AutoCheckoutOnInvalidData) {
|
45 | path.push(",AutoCheckoutOnInvalidData=true");
|
46 | }
|
47 | if (!stringIsNullOrEmpty(parameters.XorHash)) {
|
48 | path.push(`,XorHash=${encodePath(parameters.XorHash)}`);
|
49 | }
|
50 | }
|
51 | path.push(")");
|
52 | const resp = await spPost(Files(this, path.join("")), { body: content });
|
53 | return {
|
54 | data: resp,
|
55 | file: fileFromServerRelativePath(this, resp.ServerRelativeUrl),
|
56 | };
|
57 | }
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | async addChunked(url, content, progress, shouldOverWrite = true, chunkSize = 10485760) {
|
69 | const response = await spPost(Files(this, `add(overwrite=${shouldOverWrite},url='${encodePath(url)}')`));
|
70 | const file = fileFromServerRelativePath(this, response.ServerRelativeUrl);
|
71 | file.using(CancelAction(() => {
|
72 | return File(file).delete();
|
73 | }));
|
74 | return file.setContentChunked(content, progress, chunkSize);
|
75 | }
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | async addTemplateFile(fileUrl, templateFileType) {
|
84 | const response = await spPost(Files(this, `addTemplateFile(urloffile='${encodePath(fileUrl)}',templatefiletype=${templateFileType})`));
|
85 | return {
|
86 | data: response,
|
87 | file: fileFromServerRelativePath(this, response.ServerRelativeUrl),
|
88 | };
|
89 | }
|
90 | };
|
91 | __decorate([
|
92 | cancelableScope
|
93 | ], _Files.prototype, "addUsingPath", null);
|
94 | __decorate([
|
95 | cancelableScope
|
96 | ], _Files.prototype, "addChunked", null);
|
97 | __decorate([
|
98 | cancelableScope
|
99 | ], _Files.prototype, "addTemplateFile", null);
|
100 | _Files = __decorate([
|
101 | defaultPath("files")
|
102 | ], _Files);
|
103 | export { _Files };
|
104 | export const Files = spInvokableFactory(_Files);
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | export class _File extends ReadableFile {
|
110 | constructor() {
|
111 | super(...arguments);
|
112 | this.delete = deleteableWithETag();
|
113 | }
|
114 | |
115 |
|
116 |
|
117 |
|
118 | get listItemAllFields() {
|
119 | return SPInstance(this, "listItemAllFields");
|
120 | }
|
121 | |
122 |
|
123 |
|
124 |
|
125 | get versions() {
|
126 | return Versions(this);
|
127 | }
|
128 | |
129 |
|
130 |
|
131 |
|
132 | async getLockedByUser() {
|
133 | const u = await spGet(File(this, "lockedByUser"));
|
134 | if (u["odata.null"] === true) {
|
135 | return null;
|
136 | }
|
137 | else {
|
138 | return u;
|
139 | }
|
140 | }
|
141 | |
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | approve(comment = "") {
|
148 | return spPost(File(this, `approve(comment='${encodePath(comment)}')`));
|
149 | }
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | cancelUpload(uploadId) {
|
160 | return spPost(File(this, `cancelUpload(uploadId=guid'${uploadId}')`));
|
161 | }
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | checkin(comment = "", checkinType = CheckinType.Major) {
|
169 | if (comment.length > 1023) {
|
170 | throw Error("The maximum comment length is 1023 characters.");
|
171 | }
|
172 | return spPost(File(this, `checkin(comment='${encodePath(comment)}',checkintype=${checkinType})`));
|
173 | }
|
174 | |
175 |
|
176 |
|
177 | checkout() {
|
178 | return spPost(File(this, "checkout"));
|
179 | }
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | copyTo(url, shouldOverWrite = true) {
|
187 | return spPost(File(this, `copyTo(strnewurl='${encodePath(url)}',boverwrite=${shouldOverWrite})`));
|
188 | }
|
189 | async copyByPath(destUrl, ...rest) {
|
190 | let options = {
|
191 | ShouldBypassSharedLocks: true,
|
192 | ResetAuthorAndCreatedOnCopy: true,
|
193 | KeepBoth: false,
|
194 | };
|
195 | if (rest.length === 2) {
|
196 | if (typeof rest[1] === "boolean") {
|
197 | options.KeepBoth = rest[1];
|
198 | }
|
199 | else if (typeof rest[1] === "object") {
|
200 | options = { ...options, ...rest[1] };
|
201 | }
|
202 | }
|
203 | return this.moveCopyImpl(destUrl, options, rest[0], "CopyFileByPath");
|
204 | }
|
205 | |
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | deny(comment = "") {
|
212 | if (comment.length > 1023) {
|
213 | throw Error("The maximum comment length is 1023 characters.");
|
214 | }
|
215 | return spPost(File(this, `deny(comment='${encodePath(comment)}')`));
|
216 | }
|
217 | async moveByPath(destUrl, ...rest) {
|
218 | let options = {
|
219 | KeepBoth: false,
|
220 | ShouldBypassSharedLocks: true,
|
221 | RetainEditorAndModifiedOnMove: false,
|
222 | };
|
223 | if (rest.length === 2) {
|
224 | if (typeof rest[1] === "boolean") {
|
225 | options.KeepBoth = rest[1];
|
226 | }
|
227 | else if (typeof rest[1] === "object") {
|
228 | options = { ...options, ...rest[1] };
|
229 | }
|
230 | }
|
231 | return this.moveCopyImpl(destUrl, options, rest[0], "MoveFileByPath");
|
232 | }
|
233 | |
234 |
|
235 |
|
236 |
|
237 |
|
238 | publish(comment = "") {
|
239 | if (comment.length > 1023) {
|
240 | throw Error("The maximum comment length is 1023 characters.");
|
241 | }
|
242 | return spPost(File(this, `publish(comment='${encodePath(comment)}')`));
|
243 | }
|
244 | |
245 |
|
246 |
|
247 |
|
248 |
|
249 | recycle() {
|
250 | return spPost(File(this, "recycle"));
|
251 | }
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 | async deleteWithParams(parameters) {
|
258 | return spPost(File(this, "DeleteWithParameters"), body({ parameters }));
|
259 | }
|
260 | |
261 |
|
262 |
|
263 |
|
264 | undoCheckout() {
|
265 | return spPost(File(this, "undoCheckout"));
|
266 | }
|
267 | |
268 |
|
269 |
|
270 |
|
271 |
|
272 | unpublish(comment = "") {
|
273 | if (comment.length > 1023) {
|
274 | throw Error("The maximum comment length is 1023 characters.");
|
275 | }
|
276 | return spPost(File(this, `unpublish(comment='${encodePath(comment)}')`));
|
277 | }
|
278 | |
279 |
|
280 |
|
281 |
|
282 | async exists() {
|
283 | try {
|
284 | const r = await File(this).select("Exists")();
|
285 | return r.Exists;
|
286 | }
|
287 | catch (e) {
|
288 |
|
289 |
|
290 | return false;
|
291 | }
|
292 | }
|
293 | |
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | async setContent(content) {
|
300 | await spPost(File(this, "$value"), {
|
301 | body: content,
|
302 | headers: {
|
303 | "X-HTTP-Method": "PUT",
|
304 | },
|
305 | });
|
306 | return File(this);
|
307 | }
|
308 | |
309 |
|
310 |
|
311 | async getItem(...selects) {
|
312 | const q = this.listItemAllFields;
|
313 | const d = await q.select(...selects)();
|
314 | return Object.assign(Item([this, odataUrlFrom(d)]), d);
|
315 | }
|
316 | |
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 | async setContentChunked(file, progress, chunkSize = 10485760) {
|
324 | if (!isFunc(progress)) {
|
325 | progress = () => null;
|
326 | }
|
327 | const fileSize = (file === null || file === void 0 ? void 0 : file.size) || file.length;
|
328 | const totalBlocks = parseInt((fileSize / chunkSize).toString(), 10) + ((fileSize % chunkSize === 0) ? 1 : 0);
|
329 | const uploadId = getGUID();
|
330 | const fileRef = File(this).using(CancelAction(() => {
|
331 | return File(fileRef).cancelUpload(uploadId);
|
332 | }));
|
333 |
|
334 | progress({ uploadId, blockNumber: 1, chunkSize, currentPointer: 0, fileSize, stage: "starting", totalBlocks });
|
335 | let currentPointer = await fileRef.startUpload(uploadId, file.slice(0, chunkSize));
|
336 |
|
337 | for (let i = 2; i < totalBlocks; i++) {
|
338 | progress({ uploadId, blockNumber: i, chunkSize, currentPointer, fileSize, stage: "continue", totalBlocks });
|
339 | currentPointer = await fileRef.continueUpload(uploadId, currentPointer, file.slice(currentPointer, currentPointer + chunkSize));
|
340 | }
|
341 | progress({ uploadId, blockNumber: totalBlocks, chunkSize, currentPointer, fileSize, stage: "finishing", totalBlocks });
|
342 | return fileRef.finishUpload(uploadId, currentPointer, file.slice(currentPointer));
|
343 | }
|
344 | |
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | async startUpload(uploadId, fragment) {
|
359 | let n = await spPost(File(this, `startUpload(uploadId=guid'${uploadId}')`), { body: fragment });
|
360 | if (typeof n === "object") {
|
361 |
|
362 |
|
363 | n = n.StartUpload;
|
364 | }
|
365 | return parseFloat(n);
|
366 | }
|
367 | |
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 | async continueUpload(uploadId, fileOffset, fragment) {
|
379 | let n = await spPost(File(this, `continueUpload(uploadId=guid'${uploadId}',fileOffset=${fileOffset})`), { body: fragment });
|
380 | if (typeof n === "object") {
|
381 |
|
382 |
|
383 | n = n.ContinueUpload;
|
384 | }
|
385 | return parseFloat(n);
|
386 | }
|
387 | |
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 | async finishUpload(uploadId, fileOffset, fragment) {
|
398 | const response = await spPost(File(this, `finishUpload(uploadId=guid'${uploadId}',fileOffset=${fileOffset})`), { body: fragment });
|
399 | return {
|
400 | data: response,
|
401 | file: fileFromServerRelativePath(this, response.ServerRelativeUrl),
|
402 | };
|
403 | }
|
404 | moveCopyImpl(destUrl, options, overwrite, methodName) {
|
405 |
|
406 | const poster = File(this);
|
407 |
|
408 | poster.on.pre(noInherit(async (url, init, result) => {
|
409 | const { ServerRelativeUrl: srcUrl, ["odata.id"]: absoluteUrl } = await File(this).using(BatchNever()).select("ServerRelativeUrl")();
|
410 | const webBaseUrl = new URL(extractWebUrl(absoluteUrl));
|
411 | url = combine(webBaseUrl.toString(), `/_api/SP.MoveCopyUtil.${methodName}(overwrite=@a1)?@a1=${overwrite}`);
|
412 | init = body({
|
413 | destPath: toResourcePath(isUrlAbsolute(destUrl) ? destUrl : `${webBaseUrl.protocol}//${webBaseUrl.host}${destUrl}`),
|
414 | options,
|
415 | srcPath: toResourcePath(isUrlAbsolute(srcUrl) ? srcUrl : `${webBaseUrl.protocol}//${webBaseUrl.host}${srcUrl}`),
|
416 | }, init);
|
417 | return [url, init, result];
|
418 | }));
|
419 | return spPost(poster).then(() => fileFromPath(this, destUrl));
|
420 | }
|
421 | }
|
422 | __decorate([
|
423 | cancelableScope
|
424 | ], _File.prototype, "copyByPath", null);
|
425 | __decorate([
|
426 | cancelableScope
|
427 | ], _File.prototype, "moveByPath", null);
|
428 | __decorate([
|
429 | cancelableScope
|
430 | ], _File.prototype, "setContentChunked", null);
|
431 | export const File = spInvokableFactory(_File);
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 | export function fileFromServerRelativePath(base, serverRelativePath) {
|
440 | return File([base, extractWebUrl(base.toUrl())], `_api/web/getFileByServerRelativePath(decodedUrl='${encodePath(serverRelativePath)}')`);
|
441 | }
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 | export async function fileFromAbsolutePath(base, absoluteFilePath) {
|
450 | const { WebFullUrl } = await File(this).using(BatchNever()).getContextInfo(absoluteFilePath);
|
451 | const { pathname } = new URL(absoluteFilePath);
|
452 | return fileFromServerRelativePath(File([base, combine(WebFullUrl, "_api/web")]), decodeURIComponent(pathname));
|
453 | }
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 | export async function fileFromPath(base, path) {
|
462 | return (isUrlAbsolute(path) ? fileFromAbsolutePath : fileFromServerRelativePath)(base, path);
|
463 | }
|
464 |
|
465 |
|
466 |
|
467 |
|
468 | let _Versions = class _Versions extends _SPCollection {
|
469 | |
470 |
|
471 |
|
472 |
|
473 |
|
474 | getById(versionId) {
|
475 | return Version(this).concat(`(${versionId})`);
|
476 | }
|
477 | |
478 |
|
479 |
|
480 |
|
481 | deleteAll() {
|
482 | return spPost(Versions(this, "deleteAll"));
|
483 | }
|
484 | |
485 |
|
486 |
|
487 |
|
488 |
|
489 | deleteById(versionId) {
|
490 | return spPost(Versions(this, `deleteById(vid=${versionId})`));
|
491 | }
|
492 | |
493 |
|
494 |
|
495 |
|
496 |
|
497 | recycleByID(versionId) {
|
498 | return spPost(Versions(this, `recycleByID(vid=${versionId})`));
|
499 | }
|
500 | |
501 |
|
502 |
|
503 |
|
504 |
|
505 | deleteByLabel(label) {
|
506 | return spPost(Versions(this, `deleteByLabel(versionlabel='${encodePath(label)}')`));
|
507 | }
|
508 | |
509 |
|
510 |
|
511 |
|
512 |
|
513 | recycleByLabel(label) {
|
514 | return spPost(Versions(this, `recycleByLabel(versionlabel='${encodePath(label)}')`));
|
515 | }
|
516 | |
517 |
|
518 |
|
519 |
|
520 |
|
521 | restoreByLabel(label) {
|
522 | return spPost(Versions(this, `restoreByLabel(versionlabel='${encodePath(label)}')`));
|
523 | }
|
524 | };
|
525 | _Versions = __decorate([
|
526 | defaultPath("versions")
|
527 | ], _Versions);
|
528 | export { _Versions };
|
529 | export const Versions = spInvokableFactory(_Versions);
|
530 |
|
531 |
|
532 |
|
533 |
|
534 | export class _Version extends ReadableFile {
|
535 | constructor() {
|
536 | super(...arguments);
|
537 | this.delete = deleteable();
|
538 | }
|
539 | }
|
540 | export const Version = spInvokableFactory(_Version);
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 | export var CheckinType;
|
548 | (function (CheckinType) {
|
549 | CheckinType[CheckinType["Minor"] = 0] = "Minor";
|
550 | CheckinType[CheckinType["Major"] = 1] = "Major";
|
551 | CheckinType[CheckinType["Overwrite"] = 2] = "Overwrite";
|
552 | })(CheckinType || (CheckinType = {}));
|
553 |
|
554 |
|
555 |
|
556 | export var MoveOperations;
|
557 | (function (MoveOperations) {
|
558 | |
559 |
|
560 |
|
561 | MoveOperations[MoveOperations["None"] = 0] = "None";
|
562 | |
563 |
|
564 |
|
565 | MoveOperations[MoveOperations["Overwrite"] = 1] = "Overwrite";
|
566 | |
567 |
|
568 |
|
569 | MoveOperations[MoveOperations["AllowBrokenThickets"] = 8] = "AllowBrokenThickets";
|
570 | |
571 |
|
572 |
|
573 | MoveOperations[MoveOperations["RetainEditorAndModifiedOnMove"] = 2048] = "RetainEditorAndModifiedOnMove";
|
574 | })(MoveOperations || (MoveOperations = {}));
|
575 | export var TemplateFileType;
|
576 | (function (TemplateFileType) {
|
577 | TemplateFileType[TemplateFileType["StandardPage"] = 0] = "StandardPage";
|
578 | TemplateFileType[TemplateFileType["WikiPage"] = 1] = "WikiPage";
|
579 | TemplateFileType[TemplateFileType["FormPage"] = 2] = "FormPage";
|
580 | TemplateFileType[TemplateFileType["ClientSidePage"] = 3] = "ClientSidePage";
|
581 | })(TemplateFileType || (TemplateFileType = {}));
|
582 |
|
\ | No newline at end of file |