Search code examples
angularjscordovaionic-frameworkngcordova

Delete cacheDirectory cache cordova file system ionic


There is a mobile app in which i want to delete the cached images downloaded by a particular user after log out.Suppose user1 logged in and download few images and user2 logged in and downloaded few images.User2 should not see downloaded images of any other user.

      downloadFile : function(downloadLink, downloadFileName, downloadFileMimeType) {
                                    $ionicLoading.show({
                                        template: '<ion-spinner></ion-spinner>'
                                    });
                                    var accessToken = $window.localStorage.getItem(SYSTEM.AUTH_TOKEN);
                                    var options = {
                                        headers : {
                                            'Authorization' : 'Bearer ' + accessToken
                                        }
                                    };

                                    var ext;
                                    if (downloadFileMimeType == 'application/pdf') {
                                        ext = '.pdf';
                                    } else {
                                        ext = '.jpg';
                                    }
                                    var localPath;
                                    if(ionic.Platform.isAndroid()){
                                        localPath = cordova.file.externalCacheDirectory;
                                    }else{
                                        localPath = cordova.file.cacheDirectory;
                                    }
                                    localPath = localPath + downloadFileName.trim().replace(/\s+/g, '-') + ext;


                                    var ft = new FileTransfer();
                                    ft.download(downloadLink, localPath, function(entry) {
                                        $ionicLoading.hide();
                                        console.log("Downloading report on path - " + entry.toURL());
                                        cordova.plugins.fileOpener2.open(entry.toURL(), downloadFileMimeType, {
                                            error : function(e) {
                                                console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                                            },
                                            success : function(fileEntry) {
                                                console.log('File opened successfully');
                                            }

                                        });
                                    }, function fail(error) {
                                        $ionicLoading.hide();
                                        console.log("Error while downloading report with error code - " + error.code);
                                    }, true, options);
                                }

Solution

  • You can store files downloaded by specific user under a user specific folder in device and delete the same when the other user logs in. Else you can follow some file naming convention specific to the users while storing the files in folder and delete those files specific to particular user when the other user logs in.
    The sample snippet for directory deletion and its contents using cordova file plugin is as follows:

    function clearDirectory() {
        if (ionic.Platform.isAndroid()) {
            window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemDirSuccess, fail);
        } else {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemDirSuccess, fail);
        }
    
    function onFileSystemDirSuccess(fileSystem) {
        var entry = "";
        if (ionic.Platform.isAndroid()) {
            entry = fileSystem;
        } else {
            entry = fileSystem.root;
        }
        entry.getDirectory("DIRECTORY_TO_DELETE", {
                create: true,
                exclusive: false
            },
            function(entry) {
                entry.removeRecursively(function() {
                    console.log("Remove Recursively Succeeded");
                }, fail);
            }, getDirFail);
    }
    
    function getDirFail(error) {
        navigator.notification.alert("Error");
    };
    
    function fail(error) {
        navigator.notification.alert("Error");
    };
    

    }