/**
 * -------------------------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation.  All Rights Reserved.  Licensed under the MIT License.
 * See License in the project root for license information.
 * -------------------------------------------------------------------------------------------
 */ import { CacheItem , CacheService , CacheStore , GraphPageIterator , IGraph , prepScopes } from '@microsoft/mgt-element' ; import { DriveItem , UploadSession } from '@microsoft/microsoft-graph-types' ; import { schemas } from './cacheStores' ; import { ResponseType } from '@microsoft/microsoft-graph-client' ; import { blobToBase64 } from '../utils/Utils' ; /**
 * Object to be stored in cache
 */ interface CacheFile extends CacheItem { /**
     * stringified json representing a file
     */ file ? : string ; } /**
 * Object to be stored in cache
 */ interface CacheFileList extends CacheItem { /**
     * stringified json representing a list of files
     */ files ? : string [ ] ; /**
     * nextLink string to get next page
     */ nextLink ? : string ; } /**
 * document thumbnail object stored in cache
 */ export interface CacheThumbnail extends CacheItem { /**
     * tag associated with thumbnail
     */ eTag ? : string ; /**
     * document thumbnail
     */ thumbnail ? : string ; } /**
 * Clear Cache of FileList
 */ export function clearFilesCache ( ) { let cache ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , schemas . fileLists . stores . fileLists ) ; cache . clearStore ( ) ; } /**
 * Defines the time it takes for objects in the cache to expire
 */ export const getFileInvalidationTime = ( ) : number => CacheService . config . files . invalidationPeriod || CacheService . config . defaultInvalidationPeriod ; /**
 * Whether or not the cache is enabled
 */ export const getIsFilesCacheEnabled = ( ) : boolean => CacheService . config . files . isEnabled && CacheService . config . isEnabled ; /**
 * Defines the time it takes for objects in the cache to expire
 */ export const getFileListInvalidationTime = ( ) : number => CacheService . config . fileLists . invalidationPeriod || CacheService . config . defaultInvalidationPeriod ; /**
 * Whether or not the cache is enabled
 */ export const getIsFileListsCacheEnabled = ( ) : boolean => CacheService . config . fileLists . isEnabled && CacheService . config . isEnabled ; export async function getDriveItemByQuery ( graph , resource ) { let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . fileQueries ) ; const cachedFile = await getFileFromCache ( cache , resource ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( resource ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( resource , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getDriveItemById ( graph , driveId , itemId ) { const endpoint = `/drives/${ driveId }/items/${ itemId }` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . driveFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getDriveItemByPath ( graph , driveId , itemPath ) { const endpoint = `/drives/${ driveId }/root:/${ itemPath }` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . driveFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getGroupDriveItemById ( graph , groupId , itemId ) { const endpoint = `/groups/${ groupId }/drive/items/${ itemId }` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . groupFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getGroupDriveItemByPath ( graph , groupId , itemPath ) { const endpoint = `/groups/${ groupId }/drive/root:/${ itemPath }` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . groupFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getMyDriveItemById ( graph , itemId ) { const endpoint = `/me/drive/items/${ itemId }` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . userFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getMyDriveItemByPath ( graph , itemPath ) { const endpoint = `/me/drive/root:/${ itemPath }` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . userFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getSiteDriveItemById ( graph , siteId , itemId ) { const endpoint = `/sites/${ siteId }/drive/items/${ itemId }` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . siteFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getSiteDriveItemByPath ( graph , siteId , itemPath ) { const endpoint = `/sites/${ siteId }/drive/root:/${ itemPath }` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . siteFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getListDriveItemById ( graph , siteId , listId , itemId ) { const endpoint = `/sites/${ siteId }/lists/${ listId }/items/${ itemId }/driveItem` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . siteFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getUserDriveItemById ( graph , userId , itemId ) { const endpoint = `/users/${ userId }/drive/items/${ itemId }` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . userFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getUserDriveItemByPath ( graph , userId , itemPath ) { const endpoint = `/users/${ userId }/drive/root:/${ itemPath }` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . userFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'files.read' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getMyInsightsDriveItemById ( graph , insightType , id ) { const endpoint = `/me/insights/${ insightType }/${ id }/resource` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . insightFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'sites.read.all' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getUserInsightsDriveItemById ( graph , userId , insightType , id ) { const endpoint = `/users/${ userId }/insights/${ insightType }/${ id }/resource` ; let cache ; cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . insightFiles ) ; const cachedFile = await getFileFromCache ( cache , endpoint ) ; if ( cachedFile ) { return cachedFile ; } const scopes = 'sites.read.all' ; let response ; try { response = await graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( endpoint , { file : JSON . stringify ( response ) } ) ; } } catch { } return response || null ; } export async function getFilesIterator ( graph , top ? ) { const endpoint = '/me/drive/root/children' ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getDriveFilesByIdIterator ( graph , driveId , itemId , top ? ) { const endpoint = `/drives/${ driveId }/items/${ itemId }/children` ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getDriveFilesByPathIterator ( graph , driveId , itemPath , top ? ) { const endpoint = `/drives/${ driveId }/root:/${ itemPath }:/children` ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getGroupFilesByIdIterator ( graph , groupId , itemId , top ? ) { const endpoint = `/groups/${ groupId }/drive/items/${ itemId }/children` ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getGroupFilesByPathIterator ( graph , groupId , itemPath , top ? ) { const endpoint = `/groups/${ groupId }/drive/root:/${ itemPath }:/children` ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getFilesByIdIterator ( graph , itemId , top ? ) { const endpoint = `/me/drive/items/${ itemId }/children` ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getFilesByPathIterator ( graph , itemPath , top ? ) { const endpoint = `/me/drive/root:/${ itemPath }:/children` ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getSiteFilesByIdIterator ( graph , siteId , itemId , top ? ) { const endpoint = `/sites/${ siteId }/drive/items/${ itemId }/children` ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getSiteFilesByPathIterator ( graph , siteId , itemPath , top ? ) { const endpoint = `/sites/${ siteId }/drive/root:/${ itemPath }:/children` ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getUserFilesByIdIterator ( graph , userId , itemId , top ? ) { const endpoint = `/users/${ userId }/drive/items/${ itemId }/children` ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getUserFilesByPathIterator ( graph , userId , itemPath , top ? ) { const endpoint = `/users/${ userId }/drive/root:/${ itemPath }:/children` ; let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = 'files.read' ; let request ; try { request = graph . api ( endpoint ) . middlewareOptions ( prepScopes ( scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getFilesByListQueryIterator ( graph , listQuery , top ? ) { let filesPageIterator ; let cache ; const cacheStore = schemas . fileLists . stores . fileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , listQuery ) ; if ( fileList ) { filesPageIterator = await getFilesPageIteratorFromCache ( graph , fileList . files , fileList . nextLink ) ; return filesPageIterator ; } const scopes = [ 'files.read' , 'sites.read.all' ] ; let request ; try { request = await graph . api ( listQuery ) . middlewareOptions ( prepScopes ( ... scopes ) ) ; if ( top ) { request . top ( top ) ; } filesPageIterator = await getFilesPageIteratorFromRequest ( graph , request ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( listQuery , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } catch { } return filesPageIterator || null ; } export async function getMyInsightsFiles ( graph , insightType ) { const endpoint = `/me/insights/${ insightType }` ; let cache ; const cacheStore = schemas . fileLists . stores . insightfileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , endpoint ) ; if ( fileList ) { return fileList . files as DriveItem [ ] ; } const scopes = [ 'sites.read.all' ] ; let insightResponse ; try { insightResponse = await graph . api ( endpoint ) . filter ( `resourceReference/type eq 'microsoft.graph.driveItem'` ) . middlewareOptions ( prepScopes ( ... scopes ) ) . get ( ) ; } catch { } const result = await getDriveItemsByInsights ( graph , insightResponse , scopes ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : result } ) ; } return result || null ; } export async function getUserInsightsFiles ( graph , userId , insightType ) { let endpoint ; let filter ; if ( insightType === 'shared' ) { endpoint = `/me/insights/shared` ; filter = `((lastshared/sharedby/id eq '${ userId }') and (resourceReference/type eq 'microsoft.graph.driveItem'))` ; } else { endpoint = `/users/${ userId }/insights/${ insightType }` ; filter = `resourceReference/type eq 'microsoft.graph.driveItem'` ; } const key = `${ endpoint }?$filter=${ filter }` ; let cache ; const cacheStore = schemas . fileLists . stores . insightfileLists ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , cacheStore ) ; const fileList = await getFileListFromCache ( cache , cacheStore , key ) ; if ( fileList ) { return fileList . files as DriveItem [ ] ; } const scopes = [ 'sites.read.all' ] ; let insightResponse ; try { insightResponse = await graph . api ( endpoint ) . filter ( filter ) . middlewareOptions ( prepScopes ( ... scopes ) ) . get ( ) ; } catch { } const result = await getDriveItemsByInsights ( graph , insightResponse , scopes ) ; if ( getIsFileListsCacheEnabled ( ) ) { cache . putValue ( endpoint , { files : result } ) ; } return result || null ; } export async function getFilesByQueries ( graph , fileQueries ) { if ( ! fileQueries || fileQueries . length === 0 ) { return [ ] ; } const batch = graph . createBatch ( ) ; const files = [ ] ; const scopes = [ 'files.read' ] ; let cache ; let cachedFile ; if ( getIsFilesCacheEnabled ( ) ) { cache = CacheService . getCache < CacheFile > ( schemas . files , schemas . files . stores . fileQueries ) ; } for ( const fileQuery of fileQueries ) { if ( getIsFilesCacheEnabled ( ) ) { cachedFile = await cache . getValue ( fileQuery ) ; } if ( getIsFilesCacheEnabled ( ) && cachedFile && getFileInvalidationTime ( ) > Date . now ( ) - cachedFile . timeCached ) { files . push ( JSON . parse ( cachedFile . file ) ) ; } else if ( fileQuery !== '' ) { batch . get ( fileQuery , fileQuery , scopes ) ; } } try { const responses = await batch . executeAll ( ) ; for ( const fileQuery of fileQueries ) { const response = responses . get ( fileQuery ) ; if ( response && response . content ) { files . push ( response . content ) ; if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( fileQuery , { file : JSON . stringify ( response . content ) } ) ; } } } return files ; } catch ( _ ) { try { return Promise . all ( fileQueries . filter ( fileQuery => fileQuery && fileQuery !== '' ) . map ( async ( fileQuery ) => { const file = await getDriveItemByQuery ( graph , fileQuery ) ; if ( file ) { if ( getIsFilesCacheEnabled ( ) ) { cache . putValue ( fileQuery , { file : JSON . stringify ( file ) } ) ; } return file ; } } ) ) ; } catch ( _ ) { return [ ] ; } } } async function getDriveItemsByInsights ( graph , insightResponse , scopes ) { if ( ! insightResponse ) { return [ ] ; } const insightItems = insightResponse . value ; const batch = graph . createBatch ( ) ; const driveItems = [ ] ; for ( const item of insightItems ) { const driveItemId = item . resourceReference . id ; if ( driveItemId !== '' ) { batch . get ( driveItemId , driveItemId , scopes ) ; } } try { const driveItemResponses = await batch . executeAll ( ) ; for ( const item of insightItems ) { const driveItemResponse = driveItemResponses . get ( item . resourceReference . id ) ; if ( driveItemResponse && driveItemResponse . content ) { driveItems . push ( driveItemResponse . content ) ; } } return driveItems ; } catch ( _ ) { try { return Promise . all ( insightItems . filter ( insightItem => insightItem . resourceReference . id && insightItem . resourceReference . id !== '' ) . map ( async ( insightItem ) => { const driveItemResponses = await graph . api ( insightItem . resourceReference . id ) . middlewareOptions ( prepScopes ( ... scopes ) ) . get ( ) ; if ( driveItemResponses && driveItemResponses . length ) { return driveItemResponses [ 0 ] . content ; } } ) ) ; } catch ( _ ) { return [ ] ; } } } async function getFilesPageIteratorFromRequest ( graph , request ) { return GraphPageIterator . create < DriveItem > ( graph , request ) ; } async function getFilesPageIteratorFromCache ( graph , value , nextLink ) { return GraphPageIterator . createFromValue < DriveItem > ( graph , value , nextLink ) ; } async function getFileFromCache ( cache , key ) { if ( getIsFilesCacheEnabled ( ) ) { const file = await cache . getValue ( key ) ; if ( file && getFileInvalidationTime ( ) > Date . now ( ) - file . timeCached ) { const cachedFile = JSON . parse ( file . file ) ; return cachedFile ; } } return null ; } export async function getFileListFromCache ( cache , store , key ) { if ( ! cache ) { cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , store ) ; } if ( getIsFileListsCacheEnabled ( ) ) { const fileList = await cache . getValue ( key ) ; if ( fileList && getFileListInvalidationTime ( ) > Date . now ( ) - fileList . timeCached ) { return fileList ; } } return null ; } export async function fetchNextAndCacheForFilesPageIterator ( filesPageIterator ) { const nextLink = filesPageIterator . _nextLink ; if ( filesPageIterator . hasNext ) { await filesPageIterator . next ( ) ; } if ( getIsFileListsCacheEnabled ( ) ) { let cache ; cache = CacheService . getCache < CacheFileList > ( schemas . fileLists , schemas . fileLists . stores . fileLists ) ; const reg = /(graph.microsoft.com\/(v1.0|beta))(.*?)(?=\?)/gi ; const matches = reg . exec ( nextLink ) ; const key = matches [ 3 ] ; cache . putValue ( key , { files : filesPageIterator . value , nextLink : filesPageIterator . _nextLink } ) ; } } param { string } resource param { string [ ] } scopes returns { Promise < string > } export async function getDocumentThumbnail ( graph , resource , scopes ) { try { const response = ( await graph . api ( resource ) . responseType ( ResponseType . RAW ) . middlewareOptions ( prepScopes ( ... scopes ) ) . get ( ) ) as Response ; if ( response . status === 404 ) { return { eTag : null , thumbnail : null } ; } else if ( ! response . ok ) { return null ; } const eTag = response . headers . get ( 'eTag' ) ; const blob = await blobToBase64 ( await response . blob ( ) ) ; return { eTag , thumbnail : blob } ; } catch ( e ) { return null ; } } param graph param resource returns export async function getGraphfile ( graph , resource ) { try { const scopes = 'files.read' ; let response ; try { response = await graph . api ( resource ) . middlewareOptions ( prepScopes ( scopes ) ) . get ( ) . catch ( error => { return null ; } ) ; } catch { } return response || null ; } catch ( e ) { return null ; } } param graph param resource returns export async function getUploadSession ( graph , resource , conflictBehavior ) { try { const scopes = 'files.readwrite' ; const sessionOptions = { item : { '@microsoft.graph.conflictBehavior' : conflictBehavior === 0 || conflictBehavior === null ? 'rename' : 'replace' } } ; let response ; try { response = await graph . api ( resource ) . middlewareOptions ( prepScopes ( scopes ) ) . post ( JSON . stringify ( sessionOptions ) ) ; } catch { } return response || null ; } catch ( e ) { return null ; } } param graph param resource param file returns export async function sendFileChunck ( graph , resource , contentLength , contentRange , file ) { try { const scopes = 'files.readwrite' ; const header = { 'Content-Length' : contentLength , 'Content-Range' : contentRange } ; let response ; try { response = await graph . client . api ( resource ) . middlewareOptions ( prepScopes ( scopes ) ) . headers ( header ) . put ( file ) ; } catch { } return response || null ; } catch ( e ) { return null ; } } param graph param resource param file returns export async function sendFileContent ( graph , resource , file ) { try { const scopes = 'files.readwrite' ; let response ; try { response = await graph . client . api ( resource ) . middlewareOptions ( prepScopes ( scopes ) ) . put ( file ) ; } catch { } return response || null ; } catch ( e ) { return null ; } } param graph param resource returns export async function deleteSessionFile ( graph , resource ) { try { const scopes = 'files.readwrite' ; let response ; try { response = await graph . client . api ( resource ) . middlewareOptions ( prepScopes ( scopes ) ) . delete ( response => { return response ; } ) ; } catch { } return response || null ; } catch ( e ) { return null ; } }