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