protected override void OnCompleted(object sender, DownloadCompletedEventArgs e)
{
DownloadItemData IData = e.DownloadInfo.Data as DownloadItemData;
Media Video = IData.Media;
string Destination = e.DownloadInfo.Destination;
string DestinationExt = Path.GetExtension(Destination);
Destination = Destination.Substring(0, Destination.Length - Path.GetExtension(Destination).Length);
// Ensure download and merge succeeded.
if (!FileHasContent(e.DownloadInfo.Destination))
{
e.DownloadInfo.Status = DownloadStatus.Failed;
return;
}
// Get final file name.
DefaultMediaPath PathCalc = new DefaultMediaPath();
string NewFileName = PathCalc.GetDefaultFileName(Video.Artist, Video.Title, Video.MediaCategoryId, (MediaType)Video.MediaTypeId);
Directory.CreateDirectory(Path.GetDirectoryName(Settings.NaturalGroundingFolder + NewFileName));
Video.FileName = NewFileName + DestinationExt;
// Move file and overwrite.
string DstFile = Settings.NaturalGroundingFolder + Video.FileName;
if (File.Exists(DstFile))
{
FileOperationAPIWrapper.MoveToRecycleBin(DstFile);
}
File.Move(Destination + DestinationExt, DstFile);
// Add to database
EditVideoBusiness Business = new EditVideoBusiness();
Media ExistingData = Business.GetVideoById(Video.MediaId);
if (ExistingData != null)
{
// Edit video info.
ExistingData.FileName = Video.FileName;
ExistingData.Length = null;
ExistingData.Height = null;
Business.Save();
}
else
{
// Add new video info.
Business.AddVideo(Video);
Business.Save();
}
base.OnCompleted(sender, e);
}