diff --git a/back-end-cf/index.js b/back-end-cf/index.js index accb45a9..98a20148 100644 --- a/back-end-cf/index.js +++ b/back-end-cf/index.js @@ -95,7 +95,12 @@ async function handleRequest(request) { } // List a folder - const files = await fetchFiles(requestPath, body.passwd); + const files = await fetchFiles( + requestPath, + body.passwd, + body.skipToken, + body.orderby + ); return new Response(files, { headers: returnHeaders, }); @@ -188,7 +193,7 @@ async function authenticate(path, passwd) { } } -async function fetchFiles(path, passwd) { +async function fetchFiles(path, passwd, skipToken, orderby) { const parent = path || '/'; try { await authenticate(path, passwd); @@ -201,31 +206,33 @@ async function fetchFiles(path, passwd) { } if (path === '/') path = ''; - if (path || EXPOSE_PATH) + if (path || EXPOSE_PATH) { + // if EXPOSE_PATH + path equals to an empty string, ':' will lead to an error. path = ':' + encodeURIComponent(EXPOSE_PATH + path) + ':'; - + } const accessToken = await fetchAccessToken(); - const expand = - '/children?select=name,size,parentReference,lastModifiedDateTime,@microsoft.graph.downloadUrl&$top=200'; + const expand = `/children?select=name,size,parentReference,lastModifiedDateTime,@microsoft.graph.downloadUrl + &$top=200${orderby ? '&$orderby=' + orderby : ''}${ + skipToken ? '&skiptoken=' + skipToken : '' + }`; const uri = OAUTH.apiUrl + path + expand; - let pageRes = await getContent(uri, { + const pageRes = await getContent(uri, { Authorization: 'Bearer ' + accessToken, }); if (pageRes.error) { throw new Error('request failed'); } - let children = pageRes.value; - while (pageRes['@odata.nextLink']) { - pageRes = await getContent(pageRes['@odata.nextLink'], { - Authorization: 'Bearer ' + accessToken, - }); - children = children.concat(pageRes.value); - } + skipToken = pageRes['@odata.nextLink'] + ? new URL(pageRes['@odata.nextLink']).searchParams.get('$skiptoken') + : ''; + const children = pageRes.value; return JSON.stringify({ parent, + skipToken, + orderby, files: children .map((file) => ({ name: file.name, @@ -246,7 +253,8 @@ async function downloadFile(filePath, format, stream) { filePath = encodeURIComponent(`${EXPOSE_PATH}${filePath}`); const uri = `${OAUTH.apiUrl}:${filePath}:/content` + - (format ? `?format=${format}` : ''); + (format ? `?format=${format}` : '') + + (format === 'jpg' ? '&width=30000&height=30000' : ''); const accessToken = await fetchAccessToken(); return cacheFetch(uri, { diff --git a/front-end/index.html b/front-end/index.html index 676cc0b9..430ce2a4 100644 --- a/front-end/index.html +++ b/front-end/index.html @@ -5,8 +5,7 @@ window.GLOBAL_CONFIG = { SCF_GATEWAY: `//tight-bar-e3b3.logi.workers.dev`, // Worker 或云函数网关地址 SITE_NAME: `FODI`, // 站点名称 - PRELOAD: false, - IS_CF: true, + PRELOAD: false, // 是否预加载文件列表 }; @@ -719,7 +718,7 @@ function renderPage(data, cache) { let files; if (data) { - files = JSON.parse(data); + files = typeof data === 'string' ? JSON.parse(data) : data; window.fileCache.set(files.parent, files); preCache(files, 0); } else { @@ -1074,7 +1073,7 @@ input.placeholder = '密码错误'; } else { window.fileCache.set(newFiles.parent, newFiles); - window.fileCache.set(`${newFiles.parent}/.upload`, passwd); + window.fileCache.set(`${newFiles.parent}/.password`, passwd); fetchFileList(newFiles.parent); } } @@ -1656,7 +1655,38 @@ }); } - function fetchFileList(path) { + function sortList(clickedElem) { + const loadedPages = window.fileCache.get( + window.backFordwardCache.current + ); + const oldSortOrder = loadedPages?.orderby || 'name asc'; + const sortField = clickedElem.className; + const sortOrder = oldSortOrder.split(' ')[1] === 'asc' ? 'desc' : 'asc'; + const newSortOrder = `${sortField} ${sortOrder}`; + if (oldSortOrder === newSortOrder) { + return; + } + if (loadedPages.skipToken) { + window.fileCache.set(window.backFordwardCache.current, false); + fetchFileList( + window.backFordwardCache.current, + newSortOrder.replace('time', 'lastModifiedDateTime') + ); + } else { + alert(newSortOrder); + loadedPages.orderby = newSortOrder; + loadedPages.files.sort((a, b) => { + if (a[sortField] < b[sortField]) + return sortOrder === 'asc' ? -1 : 1; + if (a[sortField] > b[sortField]) + return sortOrder === 'asc' ? 1 : -1; + return 0; + }); + renderPage(loadedPages); + } + } + + function fetchFileList(path, orderby) { // console.log('fetching ' + path); const loading = document.querySelector('.loading-wrapper'); loading.dataset.hidden = '0'; @@ -1674,7 +1704,7 @@ sendRequest( window.api.method, window.api.url, - window.api.formatPayload(path), + window.api.formatPayload(path, '', { orderby: orderby }), window.api.headers, renderPage, () => { @@ -1724,7 +1754,7 @@ window.api.url + '?upload', window.api.formatPayload( odPath, - window.fileCache.get(`${odPath}/.upload`) || '', + window.fileCache.get(`${odPath}/.password`), { files: currentPage, } @@ -1878,7 +1908,6 @@ JSON.stringify({ path, passwd, - ...window.api.accessToken, ...kvs, }), headers: { @@ -1898,28 +1927,51 @@ const initialPath = new URLSearchParams(window.location.search).get('path') || window.api.root; - if (window.GLOBAL_CONFIG.IS_CF) { - fetchFileList(initialPath); - addBackForwardListener(); - addFileUploadListener(); - } else { - sendRequest( - window.api.method, - window.api.url + '?accessToken', - null, - window.api.headers, - (data) => { - const accessToken = JSON.parse(data); - window.api.accessToken = { - encrypted: accessToken.encrypted, - plain: accessToken.plain, - }; - fetchFileList(initialPath); - addBackForwardListener(); - addFileUploadListener(); - } + fetchFileList(initialPath); + addBackForwardListener(); + addFileUploadListener(); + const rightDiv = document.querySelector('div.right'); + let isRequestInProgress = false; + rightDiv.addEventListener('scroll', function () { + const scrollPosition = rightDiv.scrollTop + rightDiv.clientHeight; + const scrollHeight = rightDiv.scrollHeight; + let loadedPages = window.fileCache.get( + window.backFordwardCache.current ); - } + if (scrollPosition >= scrollHeight - 20 && loadedPages.skipToken) { + if (isRequestInProgress) return; + isRequestInProgress = true; + sendRequest( + window.api.method, + window.api.url, + window.api.formatPayload( + loadedPages.parent, + window.fileCache.get(`${loadedPages.parent}/.password`), + { + skipToken: loadedPages.skipToken, + orderby: loadedPages.orderby, + } + ), + window.api.headers, + (data) => { + data = JSON.parse(data); + loadedPages.files = loadedPages.files.concat(data.files); + loadedPages.skipToken = data.skipToken; + window.fileCache.set( + window.backFordwardCache.current, + loadedPages + ); + renderPage(loadedPages); + isRequestInProgress = false; + }, + () => { + const loadingText = loading.querySelector('.loading'); + loadingText.innerText = 'Loading NextPage Failed!'; + isRequestInProgress = false; + } + ); + } + }); }); @@ -1982,9 +2034,9 @@