2022-11-15 16:26:19 +03:00
import ModalComponent from 'ghost-admin/components/modal-base' ;
import ghostPaths from 'ghost-admin/utils/ghost-paths' ;
2022-11-23 19:36:29 +03:00
import { GENERIC _ERROR _MESSAGE } from 'ghost-admin/services/notifications' ;
2022-11-15 16:26:19 +03:00
import { computed } from '@ember/object' ;
2023-01-18 15:28:36 +03:00
import { getErrorCode } from '../services/ajax' ;
2022-11-15 16:26:19 +03:00
import { inject } from 'ghost-admin/decorators/inject' ;
import {
isRequestEntityTooLargeError ,
isUnsupportedMediaTypeError ,
isVersionMismatchError
} from 'ghost-admin/services/ajax' ;
import { inject as service } from '@ember/service' ;
export default ModalComponent . extend ( {
ajax : service ( ) ,
notifications : service ( ) ,
store : service ( ) ,
state : 'INIT' ,
file : null ,
paramName : 'importfile' ,
importResponse : null ,
errorMessage : null ,
errorHeader : null ,
showTryAgainButton : true ,
// Allowed actions
confirm : ( ) => { } ,
config : inject ( ) ,
uploadUrl : computed ( function ( ) {
2022-11-16 16:35:53 +03:00
return ` ${ ghostPaths ( ) . apiRoot } /db ` ;
2022-11-15 16:26:19 +03:00
} ) ,
formData : computed ( 'file' , function ( ) {
let formData = new FormData ( ) ;
formData . append ( this . paramName , this . file ) ;
return formData ;
} ) ,
actions : {
setFile ( file ) {
this . set ( 'file' , file ) ;
this . generateRequest ( ) ;
} ,
reset ( ) {
this . set ( 'errorMessage' , null ) ;
this . set ( 'errorHeader' , null ) ;
this . set ( 'file' , null ) ;
this . set ( 'state' , 'INIT' ) ;
this . set ( 'showTryAgainButton' , true ) ;
} ,
closeModal ( ) {
if ( this . state !== 'UPLOADING' ) {
this . _super ( ... arguments ) ;
}
} ,
// noop - we don't want the enter key doing anything
confirm ( ) { }
} ,
generateRequest ( ) {
let ajax = this . ajax ;
let formData = this . formData ;
let url = this . uploadUrl ;
this . set ( 'state' , 'UPLOADING' ) ;
ajax . post ( url , {
data : formData ,
processData : false ,
contentType : false ,
dataType : 'text'
} ) . then ( ( ) => {
this . set ( 'state' , 'PROCESSING' ) ;
} ) . catch ( ( error ) => {
this . _uploadError ( error ) ;
this . set ( 'state' , 'ERROR' ) ;
} ) ;
} ,
_uploadError ( error ) {
let message ;
let header = 'Import error' ;
if ( isVersionMismatchError ( error ) ) {
this . notifications . showAPIError ( error ) ;
}
2023-01-18 15:28:36 +03:00
if ( getErrorCode ( error ) === 'INVALID_ZIP_FILE_NAME_ENCODING' ) {
message = 'The uploaded zip could not be read due to a long or invalid file name. Please remove any special characters from the file name, or alternatively try another archiving tool if using MacOS Archive Utility.' ;
} else if ( isUnsupportedMediaTypeError ( error ) ) {
2022-11-15 16:26:19 +03:00
message = 'The file type you uploaded is not supported.' ;
} else if ( isRequestEntityTooLargeError ( error ) ) {
message = 'The file you uploaded was larger than the maximum file size your server allows.' ;
} else {
console . error ( error ) ; // eslint-disable-line
2022-11-23 19:36:29 +03:00
message = GENERIC _ERROR _MESSAGE ;
2022-11-15 16:26:19 +03:00
}
this . set ( 'errorMessage' , message ) ;
this . set ( 'errorHeader' , header ) ;
}
} ) ;