@@ -65,7 +65,9 @@ export class CoreH5PStorage {
6565
6666 const newerPatchVersion = existingLibrary . patchversion < libraryData . patchVersion ;
6767
68- if ( ! newerPatchVersion ) {
68+ // Make sure the library is fully saved to the file system if it is present in the DB.
69+ // Some files might be missing if a previous library update was interrupted.
70+ if ( ! newerPatchVersion && await this . h5pCore . h5pFS . checkLibrary ( libraryData , siteId ) ) {
6971 // Same or older version, no need to save.
7072 libraryData . saveDependencies = false ;
7173
@@ -79,21 +81,12 @@ export class CoreH5PStorage {
7981 libraryData . metadataSettings = libraryData . metadataSettings ?
8082 CoreH5PMetadata . boolifyAndEncodeSettings ( libraryData . metadataSettings ) : undefined ;
8183
84+ // Save the library files before saving to DB, in case the app is closed while copying the files.
85+ await this . h5pCore . h5pFS . saveLibrary ( libraryData , siteId ) ;
86+
8287 // Save the library data in DB.
8388 await this . h5pFramework . saveLibraryData ( libraryData , siteId ) ;
8489
85- // Now save it in FS.
86- try {
87- await this . h5pCore . h5pFS . saveLibrary ( libraryData , siteId ) ;
88- } catch ( error ) {
89- if ( libraryData . libraryId ) {
90- // An error occurred, delete the DB data because the lib FS data has been deleted.
91- await this . h5pFramework . deleteLibrary ( libraryData . libraryId , siteId ) ;
92- }
93-
94- throw error ;
95- }
96-
9790 if ( libraryData . libraryId !== undefined ) {
9891 const promises : Promise < void > [ ] = [ ] ;
9992
@@ -196,21 +189,15 @@ export class CoreH5PStorage {
196189
197190 content . params = JSON . stringify ( data . contentJsonData ) ;
198191
199- // Save the content data in DB.
200- await this . h5pCore . saveContent ( content , folderName , fileUrl , siteId ) ;
201-
202192 // Save the content files in their right place in FS.
203193 const destFolder = CorePath . concatenatePaths ( CoreFileProvider . TMPFOLDER , 'h5p/' + folderName ) ;
204194 const contentPath = CorePath . concatenatePaths ( destFolder , 'content' ) ;
205195
206- try {
207- await this . h5pCore . h5pFS . saveContent ( contentPath , folderName , siteId ) ;
208- } catch ( error ) {
209- // An error occurred, delete the DB data because the content files have been deleted.
210- await this . h5pFramework . deleteContentData ( content . id ! , siteId ) ;
196+ // Save the content files before saving to DB, in case the app is closed while copying the files.
197+ await this . h5pCore . h5pFS . saveContent ( contentPath , folderName , siteId ) ;
211198
212- throw error ;
213- }
199+ // Save the content data in DB.
200+ await this . h5pCore . saveContent ( content , folderName , fileUrl , siteId ) ;
214201 }
215202
216203 return content ;
0 commit comments