Exemplo n.º 1
0
        public Task <DownloadJob> QueueSongAsync(SongDownload song)
        {
            if (song == null)
            {
                throw new ArgumentNullException(nameof(song), "song cannot be null for SongDownloadManager.QueueSongAsync.");
            }
            //if (downloadUri == null)
            //    throw new ArgumentNullException(nameof(downloadUri), "downloadUri cannot be null for SongDownloadManager.QueueSongAsync.");
            var downloadUri = new Uri(BeatSaver_Hash_Download_Url_Base + song.Hash.ToLower());
            var newJob      = new DownloadJob(song, downloadUri, TempDirectory);

            return(QueueJobAsync(newJob));
        }
Exemplo n.º 2
0
 public DownloadJob(SongDownload song, Uri downloadUri, string tempDirectory)
 {
     if (song == null)
     {
         throw new ArgumentNullException(nameof(song), "song cannot be null for SongDownloadManager.QueueSongAsync.");
     }
     if (string.IsNullOrEmpty(tempDirectory?.Trim()))
     {
         throw new ArgumentNullException(nameof(tempDirectory), "tempDirectory cannot be null for SongDownloadManager.QueueSongAsync.");
     }
     DownloadUri   = downloadUri ?? throw new ArgumentNullException(nameof(downloadUri), "downloadUri cannot be null for SongDownloadManager.QueueSongAsync.");
     Song          = song;
     TempDirectory = tempDirectory;
     SongDirectory = Path.Combine(TempDirectory, Song.Hash);
 }
Exemplo n.º 3
0
 public Task <DownloadResult> DownloadSongAsync(SongDownload song, ICollection <ISongDownloadTarget> targets)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 4
0
 public Task <DownloadResult> DownloadSongAsync(SongDownload song, ICollection <ISongDownloadTarget> targets, Action <int> Progress, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 5
0
        public async Task <bool> TransferSong(SongDownload songs)
        {
            var test = await TransferSong(songs, false, null, CancellationToken.None).ConfigureAwait(false);

            return(test);
        }
Exemplo n.º 6
0
 public Task <bool> TransferSong(SongDownload songs, bool overwrite)
 {
     return(TransferSong(songs, overwrite, null, CancellationToken.None));
 }
Exemplo n.º 7
0
 public Task <bool> TransferSong(SongDownload songs, Action <int> SongProgressPercent, CancellationToken cancellationToken)
 {
     return(TransferSong(songs, false, SongProgressPercent, cancellationToken));
 }
Exemplo n.º 8
0
        public async Task <bool> TransferSong(SongDownload song, bool overwrite, Action <int> ProgressPercent, CancellationToken cancellationToken)
        {
            if (song == null)
            {
                throw new ArgumentNullException(nameof(song), "song cannot be null for LocalDirectoryTarget.TransferSong");
            }
            var  createdFiles     = new List <FileInfo>();
            bool cancelled        = false;
            bool targetDirCreated = false;

            if (!(song.LocalDirectory.Parent.Exists))
            {
                throw new ArgumentException("song's LocalDirectory directory cannot be null for LocalDirectoryTarget.TransferSong", nameof(song));
            }
            var sourceDir = song.LocalDirectory;
            var targetDir = new DirectoryInfo(Path.Combine(TargetDirectory.FullName, sourceDir.Name));

            if (!targetDir.Exists)
            {
                targetDir.Create();
                targetDirCreated = true;
            }
            if (!sourceDir.Exists)
            {
                throw new ArgumentException(string.Format("source directory does not exist: {0}", song), nameof(song));
            }
            bool success   = true;
            var  files     = sourceDir.EnumerateFiles().Select(f => f.FullName);
            int  fileCount = files.Count();
            int  fileNum   = 1;

            foreach (string sourceFile in files)
            {
                if (!success)
                {
                    success = false;
                    break;
                }
                if (cancellationToken.IsCancellationRequested)
                {
                    cancelled = true;
                    success   = false;
                    break;
                }

                var destFile = new FileInfo(Path.Combine(targetDir.FullName, sourceFile.Substring(sourceFile.LastIndexOf('\\') + 1)));
                if (!overwrite && destFile.Exists)
                {
                    continue;
                }
                using (FileStream SourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (FileStream DestinationStream = File.Create(destFile.FullName))
                    {
                        await SourceStream.CopyToAsync(DestinationStream).ConfigureAwait(false);
                    }
                }
                destFile.Refresh();
                if (!destFile.Exists)
                {
                    success = false;
                }
                else
                {
                    createdFiles.Add(destFile);
                    destFile.CreationTimeUtc  = File.GetCreationTimeUtc(sourceFile);
                    destFile.LastWriteTimeUtc = File.GetLastWriteTimeUtc(sourceFile);
                }
                ProgressPercent?.Invoke(fileNum * 100 / fileCount);
                fileNum++;
            }
            if (success)
            {
                // Check files are valid?
            }
            else // Copy failed, clean up
            {
                ProgressPercent?.Invoke(100);
                if (targetDirCreated)
                {
                    targetDir.Delete(true);
                }
                else
                {
                    foreach (var file in createdFiles)
                    {
                        file.Delete();
                    }
                }
            }
            if (cancelled)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }
            return(success);
        }