遠端, 資料夾遍歷

cloudspider發表於2020-11-25
    syncFolderPathList(folderPath, rootPath, parentId, pageNum, folderList, callback) {
        if(this.stopCreatingFlag){
            Logger.log('stop creat download job');
            return;
        }

        let i = 0, length = folderList.length;
        let getSubChild = (cb) => {
            let folder = folderList[i];
            this.addDownloadInfo(folder);
            let info = { ...folder, page: 1, pageNum: pageNum || PAGE_NUM }
            this.serialFolderPathList(folderPath, rootPath, parentId, info, () => {
                i++;
                if (i < length) {
                    getSubChild(cb);
                } else {
                    cb && cb();
                }
            })
        }
        
        if (i < length) {
            getSubChild(() => {
                callback && callback();
            });
        }
    }

//folder deep walk, serial handle
    serialFolderPathList(folderPath, rootPath, parentId, remotePathInfo, callback) {
        if (this.stopCreatingFlag) {
            Logger.log('stop creat download job');
            return;
        }
        const { path, page, pageNum } = remotePathInfo;

        if (Utils.isEmpty(path) || Utils.isEmpty(page)) {
            Logger.error('the folderPathList params is error', remotePathInfo);
            return;
        }

        Utils.getPathList(remotePathInfo, (error, data) => {
            if (error) {
                Logger.error('get path list error:', error);
                return;
            }

            const { pageInfo, folderList, fileList, current } = data;
            // Logger.log('folderPathList:', pageInfo, current.pathDisplay);

            if (fileList && fileList.length > 0) {
                fileList.forEach(file => {
                    //create the download job
                    this.addDownloadInfo(file)
            }

            let getNextPage = () =>{
                if (pageInfo && (pageInfo.page < pageInfo.pageTotal)) {
                    const { page, pageNum } = pageInfo;
                    let newPage = page + 1;
                    let info = { ...current, page: newPage, pageNum: pageNum || PAGE_NUM }
                    this.serialFolderPathList(folderPath, rootPath, parentId, info, callback);
                } else {
                    callback && callback();
                }   
            }

            // folder list serial recurrence start
            if (folderList && folderList.length > 0) {
                this.syncFolderPathList(folderPath, rootPath, parentId, pageNum, folderList, getNextPage);
            } else {
                getNextPage();
            }
            // folder list serial recurrence end
        })
    }

//folder deep walk,parallel handle
    parallelFolderPathList(folderPath, rootPath, remotePathInfo, callback) {
        if (this.stopCreatingFlag) {
            Logger.log('stop creat download job');
            return;
        }
        const { path, page, pageNum } = remotePathInfo;

        if (Utils.isEmpty(path) || Utils.isEmpty(page)) {
            Logger.log('the folderPathList params is error', remotePathInfo);
            return;
        }

        Utils.getPathList(remotePathInfo, (error, data) => {
            if(error){
                Logger.log('get path list error:', error);
                return;
            }

            const { pageInfo, folderList, fileList, current } = data;
            let pageLoading = false;
            let folderLoading = false;
            let count = 0;
            let checkEnd = () => {
                // ++count == folderList.length && callback();
                count++;
                if(count == folderList.length){
                    folderLoading = false;
                    if(!pageLoading) callback();
                }
            }

            let checkPage = () => {
                pageLoading = false;
                if(!folderLoading) callback();
            }

            if(fileList && fileList.length > 0){
                fileList.forEach(file => {
                    //create the download job
                    this.addDownloadInfo(file);
                })
            }

            // folder list parallel start
            let completeFlag = false;
            if (folderList && folderList.length > 0) {
                folderList.forEach(folder => {
                    //create the download job
                    folderLoading = true;
                    this.addDownloadInfo(folder);
                    let info = { ...folder, page: 1, pageNum: pageNum || PAGE_NUM }
                    this.parallelFolderPathList(folderPath, rootPath, info, checkEnd);
                    completeFlag = true;
                })
            }
            
            if (pageInfo && (pageInfo.page < pageInfo.pageTotal)) {
                pageLoading = true;
                const { page, pageNum } = pageInfo;
                let newPage = page + 1;
                let info = { ...current, page: newPage, pageNum: pageNum || PAGE_NUM }
                this.parallelFolderPathList(folderPath, rootPath, info, checkPage);
            } else {
                // no folders, last page invoke callback
                if(!completeFlag) {
                    callback && callback();
                }
            }
            // folder list parallel end
        })
    }

 

相關文章