コード例 #1
0
        public ITorrentDetails Post(TorrentAddRequest request)
        {
            // Get the uploaded torrent file.
            var uploadedTorrentFile = base.RequestContext.Files.First();
            var outputDirectoryPath = request.OutputDirectoryPath;

            // Add the torrent and get the torrent details.
            var addedTorrentId      = TorrentClient.Add(uploadedTorrentFile.InputStream, request.OutputDirectoryPath);
            var addedTorrentDetails = TorrentClient.List().First(t => t.Id == addedTorrentId);

            // Mirror.
            if (request.Mirror)
            {
                Directory.GetFiles(addedTorrentDetails.OutputDirectory, "*", SearchOption.AllDirectories).ToList().ForEach(currentFile =>
                {
                    var relativeFilePath = currentFile.Replace(addedTorrentDetails.OutputDirectory, "")
                                           .TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

                    if (!addedTorrentDetails.Files.Contains(relativeFilePath))
                    {
                        File.Delete(currentFile);
                    }
                });

                FileSystemHelpers.DeleteEmptyDirectory(addedTorrentDetails.OutputDirectory);
            }

            // Return the added torrent details.
            return(addedTorrentDetails);
        }