Ejemplo n.º 1
0
 public MediaFile(string url, FileOrigin fileOrigin, bool initializeMetadataOnDemand = true)
 {
     this.Url        = url;
     this.FileOrigin = fileOrigin;
     this.InitializeMetadataOnDemand = initializeMetadataOnDemand;
     if (!this.InitializeMetadataOnDemand)
     {
         this.metadata = this.InitializeMetadata();
     }
 }
Ejemplo n.º 2
0
        public DownloadResult Download(MediaFile file, string tempFolderPath, bool resumePreviousDownload = false)
        {
            DownloadResult result = new DownloadResult(file);

            result.IsDownloaded = true;
            MediaFileMetadata metadata   = file.Metadata;
            string            outputPath = Path.Combine(tempFolderPath, metadata.FileName + metadata.FileExtension);

            if (!this.OnMediaFileDownloadStarting(file, outputPath) || metadata == MediaFileMetadata.DefaultMetadata)
            {
                result.IsDownloaded = false;
                return(result);
            }

            file.InitializeMetadata();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(metadata.DownloadLink);

            request.Method    = WebRequestMethods.File.DownloadFile;
            request.UserAgent = MediaDownloader.GetRandomUserAgent();
            request.KeepAlive = true;
            request.Timeout   = Timeout.Infinite;
            try
            {
                bool fileExists = File.Exists(outputPath);
                if (!fileExists || !this.IsFileLocked(new FileInfo(outputPath)))
                {
                    this.CreateFileDownloadRequest(file, outputPath, request, resumePreviousDownload);
                }
                else
                {
                    result.IsDownloaded = false;
                    result.Exceptions.Add(new IOException("File {0} is locked, try killing the process that has locked it"));
                    return(result);
                }
            }
            catch (WebException webEx)
            {
                Debug.WriteLine("File " + file.Metadata.FileName + " Could not be downloaded " + webEx + " " + webEx.InnerException);
                result.IsDownloaded = false;
                result.Exceptions.Add(webEx);
            }

            result.DownloadPath = outputPath;
            return(result);
        }