示例#1
0
        internal async static Task CopyNecessaryFilesFromCurrentPackageAsync(IFile diffManifestFile, IFolder currentPackageFolder, IFolder newPackageFolder)
        {
            await FileUtils.MergeFoldersAsync(currentPackageFolder, newPackageFolder).ConfigureAwait(false);

            JObject diffManifest = await CodePushUtils.GetJObjectFromFileAsync(diffManifestFile).ConfigureAwait(false);

            var deletedFiles = (JArray)diffManifest["deletedFiles"];

            foreach (string fileNameToDelete in deletedFiles)
            {
                var fileToDelete = await newPackageFolder.GetFileAsync(fileNameToDelete).ConfigureAwait(false);

                await fileToDelete.DeleteAsync().ConfigureAwait(false);
            }
        }
        // internal async static Task CopyNecessaryFilesFromCurrentPackageAsync(IFile diffManifestFile,IFolder unzipedPackageFolder, IFolder currentPackageFolder, IFolder newPackageFolder)
        internal async static Task CopyNecessaryFilesFromCurrentPackageAsync(IFile diffManifestFile, IFolder currentPackageFolder, IFolder newPackageFolder)
        {
            await FileUtils.MergeFoldersAsync(currentPackageFolder, newPackageFolder).ConfigureAwait(false);

            JObject diffManifest = await CodePushUtils.GetJObjectFromFileAsync(diffManifestFile).ConfigureAwait(false);

            var deletedFiles = (JArray)diffManifest["deletedFiles"];

            if (deletedFiles != null)
            {
                foreach (string fileNameToDelete in deletedFiles)
                {
                    var fileToDelete = await newPackageFolder.GetFileAsync(fileNameToDelete).ConfigureAwait(false);

                    await fileToDelete.DeleteAsync().ConfigureAwait(false);
                }
            }

            // var patchedFiles = (JArray)diffManifest["patchedFiles"];
            // if (patchedFiles != null)
            // {
            //     foreach (string fileNameToPatch in patchedFiles)
            //     {
            //         var patchFile = await unzipedPackageFolder.GetFileAsync(fileNameToPatch).ConfigureAwait(false);
            //         var patchFileContent = await patchFile.ReadAllTextAsync().ConfigureAwait(false);

            //         var bundleFile = await newPackageFolder.GetFileAsync(fileNameToPatch.Replace(".patch","")).ConfigureAwait(false);
            //         var bundleFileContent = await bundleFile.ReadAllTextAsync().ConfigureAwait(false);

            //         var dmp = new DiffMatchPatch();


            //         var fileToDelete = await newPackageFolder.GetFileAsync(fileNameToPatch).ConfigureAwait(false);
            //         await fileToDelete.DeleteAsync().ConfigureAwait(false);
            //     }
            // }
        }
        internal async Task DownloadPackageAsync(JObject updatePackage, string expectedBundleFileName, Progress <HttpProgress> downloadProgress)
        {
            // Using its hash, get the folder where the new update will be saved
            StorageFolder codePushFolder = await GetCodePushFolderAsync().ConfigureAwait(false);

            var           newUpdateHash   = (string)updatePackage[CodePushConstants.PackageHashKey];
            StorageFolder newUpdateFolder = await GetPackageFolderAsync(newUpdateHash, false).ConfigureAwait(false);

            if (newUpdateFolder != null)
            {
                // This removes any stale data in newUpdateFolder that could have been left
                // uncleared due to a crash or error during the download or install process.
                await newUpdateFolder.DeleteAsync().AsTask().ConfigureAwait(false);
            }

            newUpdateFolder = await GetPackageFolderAsync(newUpdateHash, true).ConfigureAwait(false);

            StorageFile newUpdateMetadataFile = await newUpdateFolder.CreateFileAsync(CodePushConstants.PackageFileName).AsTask().ConfigureAwait(false);

            var         downloadUrlString = (string)updatePackage[CodePushConstants.DownloadUrlKey];
            StorageFile downloadFile      = await GetDownloadFileAsync().ConfigureAwait(false);

            var downloadUri = new Uri(downloadUrlString);

            // Download the file and send progress event asynchronously
            var request = new HttpRequestMessage(HttpMethod.Get, downloadUri);
            var client  = new HttpClient();
            var cancellationTokenSource = new CancellationTokenSource();

            using (HttpResponseMessage response = await client.SendRequestAsync(request).AsTask(cancellationTokenSource.Token, downloadProgress).ConfigureAwait(false))
                using (IInputStream inputStream = await response.Content.ReadAsInputStreamAsync().AsTask().ConfigureAwait(false))
                    using (IRandomAccessStream downloadFileStream = await downloadFile.OpenAsync(FileAccessMode.ReadWrite).AsTask().ConfigureAwait(false))
                    {
                        await RandomAccessStream.CopyAsync(inputStream, downloadFileStream).AsTask().ConfigureAwait(false);
                    }

            try
            {
                // Unzip the downloaded file and then delete the zip
                StorageFolder unzippedFolder = await GetUnzippedFolderAsync().ConfigureAwait(false);

                ZipFile.ExtractToDirectory(downloadFile.Path, unzippedFolder.Path);
                await downloadFile.DeleteAsync().AsTask().ConfigureAwait(false);

                // Merge contents with current update based on the manifest
                StorageFile diffManifestFile = (StorageFile)await unzippedFolder.TryGetItemAsync(CodePushConstants.DiffManifestFileName).AsTask().ConfigureAwait(false);

                if (diffManifestFile != null)
                {
                    StorageFolder currentPackageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);

                    if (currentPackageFolder == null)
                    {
                        throw new InvalidDataException("Received a diff update, but there is no current version to diff against.");
                    }

                    await UpdateUtils.CopyNecessaryFilesFromCurrentPackageAsync(diffManifestFile, currentPackageFolder, newUpdateFolder).ConfigureAwait(false);

                    await diffManifestFile.DeleteAsync().AsTask().ConfigureAwait(false);
                }

                await FileUtils.MergeFoldersAsync(unzippedFolder, newUpdateFolder).ConfigureAwait(false);

                await unzippedFolder.DeleteAsync().AsTask().ConfigureAwait(false);

                // For zip updates, we need to find the relative path to the jsBundle and save it in the
                // metadata so that we can find and run it easily the next time.
                string relativeBundlePath = await UpdateUtils.FindJSBundleInUpdateContentsAsync(newUpdateFolder, expectedBundleFileName).ConfigureAwait(false);

                if (relativeBundlePath == null)
                {
                    throw new InvalidDataException("Update is invalid - A JS bundle file named \"" + expectedBundleFileName + "\" could not be found within the downloaded contents. Please check that you are releasing your CodePush updates using the exact same JS bundle file name that was shipped with your app's binary.");
                }
                else
                {
                    if (diffManifestFile != null)
                    {
                        // TODO verify hash for diff update
                        // CodePushUpdateUtils.verifyHashForDiffUpdate(newUpdateFolderPath, newUpdateHash);
                    }

                    updatePackage[CodePushConstants.RelativeBundlePathKey] = relativeBundlePath;
                }
            }
            catch (InvalidDataException)
            {
                // Downloaded file is not a zip, assume it is a jsbundle
                await downloadFile.RenameAsync(expectedBundleFileName).AsTask().ConfigureAwait(false);

                await downloadFile.MoveAsync(newUpdateFolder).AsTask().ConfigureAwait(false);
            }

            /*TODO: ZipFile.ExtractToDirectory is not reliable and throws exceptions if:
             * - folder exists already
             * - path is too long
             * it needs to be handled
             */

            // Save metadata to the folder
            await FileIO.WriteTextAsync(newUpdateMetadataFile, JsonConvert.SerializeObject(updatePackage)).AsTask().ConfigureAwait(false);
        }
示例#4
0
        internal async Task DownloadPackageAsync(JObject updatePackage, string expectedBundleFileName, Progress <HttpProgress> downloadProgress)
        {
            // Using its hash, get the folder where the new update will be saved
            var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);

            var newUpdateHash   = (string)updatePackage[CodePushConstants.PackageHashKey];
            var newUpdateFolder = await GetPackageFolderAsync(newUpdateHash, false).ConfigureAwait(false);

            if (newUpdateFolder != null)
            {
                // This removes any stale data in newUpdateFolder that could have been left
                // uncleared due to a crash or error during the download or install process.
                await newUpdateFolder.DeleteAsync().ConfigureAwait(false);
            }

            newUpdateFolder = await GetPackageFolderAsync(newUpdateHash, true).ConfigureAwait(false);

            var newUpdateMetadataFile = await newUpdateFolder.CreateFileAsync(CodePushConstants.PackageFileName, CreationCollisionOption.ReplaceExisting).ConfigureAwait(false);

            var downloadUrlString = (string)updatePackage[CodePushConstants.DownloadUrlKey];
            var downloadFile      = await GetDownloadFileAsync().ConfigureAwait(false);

            await UpdateUtils.DownloadBundleAsync(downloadUrlString, downloadFile.Path, downloadProgress);

            try
            {
                // Unzip the downloaded file and then delete the zip
                var unzippedFolder = await CreateUnzippedFolderAsync().ConfigureAwait(false);

                /**
                 * TODO:
                 *  1) ZipFile.ExtractToDirectory is not reliable and throws exception if:
                 *      - path is too long (> 250 chars)
                 *
                 *  2) Un-zipping is quite long operation. Does it make sense for async?
                 *  await UpdateUtils.UnzipBundleAsync(downloadFile.Path, unzippedFolder.Path);
                 *
                 *  Possible implementation
                 *
                 *  internal async static Task UnzipBundleAsync(string zipFileName, string targetDir)
                 *  {
                 *    await Task.Run(() =>
                 *      {
                 *        ZipFile.ExtractToDirectory(zipFileName, targetDir)
                 *        return Task.CompletedTask;
                 *      });
                 *  }
                 */
                ZipFile.ExtractToDirectory(downloadFile.Path, unzippedFolder.Path);
                await downloadFile.DeleteAsync().ConfigureAwait(false);

                // Merge contents with current update based on the manifest
                IFile diffManifestFile = null;
                try
                {
                    diffManifestFile = await unzippedFolder.GetFileAsync(CodePushConstants.DiffManifestFileName).ConfigureAwait(false);
                }
                catch (FileNotFoundException)
                {
                    //file may not be present in folder just skip it
                }
                if (diffManifestFile != null)
                {
                    var currentPackageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);

                    if (currentPackageFolder == null)
                    {
                        throw new InvalidDataException("Received a diff update, but there is no current version to diff against.");
                    }

                    await UpdateUtils.CopyNecessaryFilesFromCurrentPackageAsync(diffManifestFile, currentPackageFolder, newUpdateFolder).ConfigureAwait(false);

                    await diffManifestFile.DeleteAsync().ConfigureAwait(false);
                }

                await FileUtils.MergeFoldersAsync(unzippedFolder, newUpdateFolder).ConfigureAwait(false);

                await unzippedFolder.DeleteAsync().ConfigureAwait(false);

                // For zip updates, we need to find the relative path to the jsBundle and save it in the
                // metadata so that we can find and run it easily the next time.
                var relativeBundlePath = await UpdateUtils.FindJSBundleInUpdateContentsAsync(newUpdateFolder, expectedBundleFileName).ConfigureAwait(false);

                if (relativeBundlePath == null)
                {
                    throw new InvalidDataException("Update is invalid - A JS bundle file named \"" + expectedBundleFileName + "\" could not be found within the downloaded contents. Please check that you are releasing your CodePush updates using the exact same JS bundle file name that was shipped with your app's binary.");
                }
                else
                {
                    if (diffManifestFile != null)
                    {
                        // TODO verify hash for diff update
                        // CodePushUpdateUtils.verifyHashForDiffUpdate(newUpdateFolderPath, newUpdateHash);
                    }

                    updatePackage[CodePushConstants.RelativeBundlePathKey] = relativeBundlePath;
                }
            }
            catch (InvalidDataException)
            {
                // Downloaded file is not a zip, assume it is a jsbundle
                await downloadFile.RenameAsync(expectedBundleFileName).ConfigureAwait(false);

                await downloadFile.MoveAsync(newUpdateFolder.Path, NameCollisionOption.ReplaceExisting).ConfigureAwait(false);
            }

            // Save metadata to the folder
            await newUpdateMetadataFile.WriteAllTextAsync(JsonConvert.SerializeObject(updatePackage)).ConfigureAwait(false);
        }