/**

Copyright 2021 Forestry.io Holdings, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/ import { MediaStore , MediaUploadOptions , Media , MediaList , MediaListOptions , } from 'tinacms' ; import { GithubClient } from '../github-client' ; import base64File from './base64File' ; import path from 'path' ; export class GithubMediaStore implements MediaStore { accept = '*' ; constructor ( private githubClient ) { } async persist ( files ) { const uploaded = [ ] ; for ( const { file , directory } of files ) { let mediaPath = path . join ( directory , file . name ) ; if ( mediaPath . charAt ( 0 ) === '/' ) { mediaPath = mediaPath . slice ( 1 ) ; } try { const content = ( await base64File ( file ) ) . toString ( ) . split ( ',' ) [ 1 ] ; const uploadResponse = await this . githubClient . upload ( mediaPath , content , 'Upload' , true ) ; uploaded . push ( contentToMedia ( uploadResponse . content ) ) ; } catch ( e ) { console . warn ( 'Failed to upload content to Github: ' + e ) ; } } return uploaded ; } async delete ( media ) { await this . githubClient . delete ( path . join ( media . directory , media . filename ) , `Deleted ${ media . filename }` ) ; } async previewSrc ( src ) { try { return this . githubClient . getDownloadUrl ( src ) ; } catch { return src ; } } async list ( options ? ) { const directory = options ?. directory ?? '' ; const offset = ( options ?. offset as number ) ?? 0 ; const limit = options ?. limit ?? 50 ; const unfilteredItems = await this . githubClient . fetchFile ( directory ) ; const items = unfilteredItems . filter ( function filterByAccept ( ) { return true ; } ) ; return { items : items . map ( contentToMedia ) . slice ( offset , offset + limit ) , nextOffset : nextOffset ( offset , limit , items . length ) , } ; } } const nextOffset = ( offset , limit , count ) => { if ( offset + limit < count ) return offset + limit ; return undefined ; } ; const contentToMedia = ( item ) : Media => { const previewable = [ '.jpg' , '.jpeg' , '.png' , '.webp' , '.svg' ] ; const mediaItem = { id : item . path , filename : item . name , directory : item . path . slice ( 0 , item . path . length - item . name . length ) , type : item . type , } ; if ( previewable . includes ( path . extname ( item . name ) . toLowerCase ( ) ) ) { mediaItem . previewSrc = item . download_url ; } return mediaItem ; } ; interface GithubUploadResponse { content : GithubContent ; } interface GithubContent { name : string ; path : string ; size : number ; type : 'file' | 'dir' ; url : string ; download_url : string ; git_url : string ; html_url : string ; }