Co-authored-by: gorbenko.pavel <gorbenko.pavel@tansu.kz> Reviewed-on: http://git.tnsu.kz/timur.gorodetskiy/request-portal/pulls/9
This commit was merged in pull request #9.
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { getSP } from '../pnp/pnpjs';
|
||||
import '@pnp/sp/lists';
|
||||
import { IList } from '@pnp/sp/lists';
|
||||
import '@pnp/sp/items';
|
||||
import '@pnp/sp/attachments';
|
||||
|
||||
export interface IAttachmentInfo {
|
||||
@@ -7,6 +10,18 @@ export interface IAttachmentInfo {
|
||||
url: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* listRef — либо Title списка данных (текущая партиция), либо его серверный
|
||||
* относительный url (конкретная, в т.ч. архивная партиция — см. shared/
|
||||
* listTitles.ts). Различаем по ведущему '/', тем же приёмом, что и
|
||||
* ApproverService.dataListRef — иначе вложения заявки, лежащей в архивной
|
||||
* партиции, читались/писались бы в список ТЕКУЩЕЙ (там либо нет элемента с
|
||||
* таким Id, либо, хуже, есть чужой элемент с тем же Id).
|
||||
*/
|
||||
function resolveList(listRef: string): IList {
|
||||
return listRef.startsWith('/') ? getSP().web.getList(listRef) : getSP().web.lists.getByTitle(listRef);
|
||||
}
|
||||
|
||||
/** Прочитать файл как ArrayBuffer (FileReader — совместимо со всеми браузерами). */
|
||||
function readAsArrayBuffer(file: File): Promise<ArrayBuffer> {
|
||||
return new Promise<ArrayBuffer>((resolve, reject) => {
|
||||
@@ -18,11 +33,8 @@ function readAsArrayBuffer(file: File): Promise<ArrayBuffer> {
|
||||
}
|
||||
|
||||
/** Вложения элемента списка (для режима согласующего — ссылки на скачивание). */
|
||||
export async function getAttachments(listTitle: string, itemId: number): Promise<IAttachmentInfo[]> {
|
||||
const files = await getSP()
|
||||
.web.lists.getByTitle(listTitle)
|
||||
.items.getById(itemId)
|
||||
.attachmentFiles();
|
||||
export async function getAttachments(listRef: string, itemId: number): Promise<IAttachmentInfo[]> {
|
||||
const files = await resolveList(listRef).items.getById(itemId).attachmentFiles();
|
||||
return files.map((f) => ({ fileName: f.FileName, url: f.ServerRelativeUrl }));
|
||||
}
|
||||
|
||||
@@ -31,14 +43,14 @@ export async function getAttachments(listTitle: string, itemId: number): Promise
|
||||
* Вызывать ПОСЛЕ создания элемента (нужен его id).
|
||||
*/
|
||||
export async function uploadAttachments(
|
||||
listTitle: string,
|
||||
listRef: string,
|
||||
itemId: number,
|
||||
files: File[]
|
||||
): Promise<void> {
|
||||
if (files.length === 0) {
|
||||
return;
|
||||
}
|
||||
const item = getSP().web.lists.getByTitle(listTitle).items.getById(itemId);
|
||||
const item = resolveList(listRef).items.getById(itemId);
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
const content = await readAsArrayBuffer(file);
|
||||
@@ -48,13 +60,9 @@ export async function uploadAttachments(
|
||||
|
||||
/** Удалить вложение элемента по имени файла (для правки заявки). */
|
||||
export async function deleteAttachment(
|
||||
listTitle: string,
|
||||
listRef: string,
|
||||
itemId: number,
|
||||
fileName: string
|
||||
): Promise<void> {
|
||||
await getSP()
|
||||
.web.lists.getByTitle(listTitle)
|
||||
.items.getById(itemId)
|
||||
.attachmentFiles.getByName(fileName)
|
||||
.delete();
|
||||
await resolveList(listRef).items.getById(itemId).attachmentFiles.getByName(fileName).delete();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user