public override IEnumerable <string> CopyFiles(
            string destination,
            IEnumerable <string> packageFiles,
            ExtractPackageFileDelegate extractFile,
            ILogger logger,
            CancellationToken token)
        {
            var filesCopied = new List <string>();

            foreach (var packageFile in packageFiles)
            {
                token.ThrowIfCancellationRequested();

                var sourceFile = GetFile(packageFile);

                var targetPath = Path.Combine(destination, packageFile);
                Directory.CreateDirectory(Path.GetDirectoryName(targetPath));

                using (var fileStream = sourceFile.OpenRead())
                {
                    targetPath = extractFile(sourceFile.FullName, targetPath, fileStream);
                    if (targetPath != null)
                    {
                        File.SetLastWriteTimeUtc(targetPath, sourceFile.LastWriteTimeUtc);
                        filesCopied.Add(targetPath);
                    }
                }
            }

            return(filesCopied);
        }
예제 #2
0
 /// <summary>
 /// Copies specified files in the package to the destination location.
 /// </summary>
 /// <param name="destination">A directory path to copy files to.</param>
 /// <param name="packageFiles">An enumerable of files in the package to copy.</param>
 /// <param name="extractFile">A package file extraction delegate.</param>
 /// <param name="logger">A logger.</param>
 /// <param name="token">A cancellation token.</param>
 /// <returns>An enumerable of file paths in the destination location.</returns>
 /// <exception cref="NotSupportedException">Thrown always.</exception>
 public override IEnumerable <string> CopyFiles(
     string destination,
     IEnumerable <string> packageFiles,
     ExtractPackageFileDelegate extractFile,
     ILogger logger,
     CancellationToken token)
 {
     throw new NotSupportedException();
 }
예제 #3
0
 public virtual Task <IEnumerable <string> > CopyFilesAsync(
     string destination,
     IEnumerable <string> packageFiles,
     ExtractPackageFileDelegate extractFile,
     ILogger logger,
     CancellationToken cancellationToken)
 {
     return(Task.FromResult(CopyFiles(destination, packageFiles, extractFile, logger, cancellationToken)));
 }
예제 #4
0
        public override IEnumerable <string> CopyFiles(
            string destination,
            IEnumerable <string> packageFiles,
            ExtractPackageFileDelegate extractFile,
            ILogger logger,
            CancellationToken token)
        {
            var filesCopied     = new List <string>();
            var pacakgeIdentity = GetIdentity();

            foreach (var packageFile in packageFiles)
            {
                token.ThrowIfCancellationRequested();

                var entry = GetEntry(packageFile);

                var packageFileName = entry.FullName;
                // An entry in a ZipArchive could start with a '/' based on how it is zipped
                // Remove it if present
                if (packageFileName.StartsWith("/", StringComparison.Ordinal))
                {
                    packageFileName = packageFileName.Substring(1);
                }

                // ZipArchive always has forward slashes in them. By replacing them with DirectorySeparatorChar;
                // in windows, we get the windows-style path
                var normalizedPath = Uri.UnescapeDataString(packageFileName.Replace('/', Path.DirectorySeparatorChar));

                destination = NormalizeDirectoryPath(destination);
                ValidatePackageEntry(destination, normalizedPath, pacakgeIdentity);

                var targetFilePath = Path.Combine(destination, normalizedPath);

                using (var stream = entry.Open())
                {
                    var copiedFile = extractFile(packageFileName, targetFilePath, stream);
                    if (copiedFile != null)
                    {
                        entry.UpdateFileTimeFromEntry(copiedFile, logger);
                        entry.UpdateFilePermissionsFromEntry(copiedFile, logger);

                        filesCopied.Add(copiedFile);
                    }
                }
            }

            return(filesCopied);
        }
예제 #5
0
        /// <summary>
        /// Asynchronously copies specified files in the package to the destination location.
        /// </summary>
        /// <param name="destination">A directory path to copy files to.</param>
        /// <param name="packageFiles">An enumerable of files in the package to copy.</param>
        /// <param name="extractFile">A package file extraction delegate.</param>
        /// <param name="logger">A logger.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>A task that represents the asynchronous operation.
        /// The task result (<see cref="Task{TResult}.Result" />) returns an
        /// <see cref="IEnumerable{String}" />.</returns>
        /// <exception cref="ArgumentException">Thrown if <paramref name="destination" />
        /// is either <c>null</c> or an empty string.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="packageFiles" />
        /// is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="logger" /> is <c>null</c>.</exception>
        /// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
        /// is cancelled.</exception>
        public override async Task <IEnumerable <string> > CopyFilesAsync(
            string destination,
            IEnumerable <string> packageFiles,
            ExtractPackageFileDelegate extractFile,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(destination))
            {
                throw new ArgumentException(Strings.ArgumentCannotBeNullOrEmpty, nameof(destination));
            }

            if (packageFiles == null)
            {
                throw new ArgumentNullException(nameof(packageFiles));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            cancellationToken.ThrowIfCancellationRequested();

            if (!packageFiles.Any())
            {
                return(Enumerable.Empty <string>());
            }

            var packageId      = _packageIdentity.Id;
            var packageVersion = _packageIdentity.Version.ToNormalizedString();
            var request        = new CopyFilesInPackageRequest(
                _packageSourceRepository,
                packageId,
                packageVersion,
                packageFiles,
                destination);
            var response = await _plugin.Connection.SendRequestAndReceiveResponseAsync <CopyFilesInPackageRequest, CopyFilesInPackageResponse>(
                MessageMethod.CopyFilesInPackage,
                request,
                cancellationToken);

            if (response != null)
            {
                switch (response.ResponseCode)
                {
                case MessageResponseCode.Success:
                    return(response.CopiedFiles);

                case MessageResponseCode.Error:
                    throw new PluginException(
                              string.Format(CultureInfo.CurrentCulture,
                                            Strings.Plugin_FailedOperationForPackage,
                                            _plugin.Name,
                                            MessageMethod.CopyFilesInPackage,
                                            packageId,
                                            packageVersion));

                case MessageResponseCode.NotFound:
                // This class is only created if a success response code is received for a
                // PrefetchPackageRequest, meaning that the plugin has already confirmed
                // that the package exists.

                default:
                    break;
                }
            }

            return(Enumerable.Empty <string>());
        }
예제 #6
0
 public abstract IEnumerable <string> CopyFiles(
     string destination,
     IEnumerable <string> packageFiles,
     ExtractPackageFileDelegate extractFile,
     ILogger logger,
     CancellationToken token);
 public override IEnumerable<string> CopyFiles(string destination, IEnumerable<string> packageFiles, ExtractPackageFileDelegate extractFile,
     Common.ILogger logger, CancellationToken token)
 {
     throw new NotImplementedException();
 }
예제 #8
0
 public IEnumerable <string> CopyFiles(string destination, IEnumerable <string> packageFiles, ExtractPackageFileDelegate extractFile, ILogger logger, CancellationToken token) => _reader.CopyFiles(destination, packageFiles, extractFile, logger, token);