hyperN
hyperN

Reputation: 2754

PhoneGap check if file exists

I want to check if file exists, and if it does not, download it. I have tried suggested solutions in this SO question and this blog post

But none of those solution worked for me. Here is my code based on SO question:

 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
                console.log("gotFS");
                getFolder(fileSystem, video.folderName, function(folder) {
                    console.log("Got folder");
                    var filePath = folder.toURL() + "\/" + "videotest.mp4";

                    fileSystem.root.getFile(filePath, { create: false }, playVideo(filePath, true), transferFile(video.uri, filePath));


                }, function() {
                    console.log("failed to get folder");
                });

            },
            function() {
                console.log("failed to get filesystem");
            });

Play video function looks like this:

 function playVideo(uri, hasBeenDownloaded) {

   console.log("was video dowloaded already", hasBeenDownloaded);
    var player = document.getElementById("videoPlayer");
    var source = document.createElement("source");

    source.src = uri;
    source.type = "video/mp4";

    player.appendChild(source);
    player.load();
}

And here is transferFile function:

function transferFile(uri, filePath) {
       var transfer = new FileTransfer();
       transfer.download(
           uri,
           filePath,
           function (entry) {
               var targetPath = entry.toURL();

               console.log("target path je", targetPath);
               document.getElementById("result").innerHTML = "File saved to: " + targetPath;
               playVideo(targetPath, false);

           },
           function (error) {
               document.getElementById("result").innerHTML = "An error has occurred: Code = " + error.code;
               console.log("download error source " + error.source);
               console.log("download error target " + error.target);
               console.log("upload error code" + error.code);
           }
           );
   }

What happens is that both playVideo and transferFile are allays called, no matter if file exists or not.

For example if file already exists, firstly only playVideo will be called and in console it will be logged:

was video dowloaded already: true

And after sometime I see that transferFile function was called, and when file is downloaded it will call playVideo and in console i can see:

was video dowloaded already: false

So why is this happening ?

P.S.

With code suggested in blog post problem is completely same, i.e. both functions are called.

Upvotes: 0

Views: 1242

Answers (1)

Raymond Camden
Raymond Camden

Reputation: 10857

Your code is passing the called function as the callback, not the function itself. Let me give an example. Imagine you have function Foo. It asks you to pass 2 callback functions - one for success, one for failure. You could do this:

function good() { alert('good'); };
function bad() { alert('bad'); };

foo(good, bad);

However, this is what you did:

foo(good(), bad());

This is why you have both being called. You need to change:

 fileSystem.root.getFile(filePath, { create: false }, playVideo(filePath, true), transferFile(video.uri, filePath));

to

fileSystem.root.getFile(filePath, { create: false }, playVideo, transferFile);

which means you'll need to access the values you need not as arguments but as regular variables.

Upvotes: 1

Related Questions