/// <summary> /// Downloads a file from a URL /// </summary> /// <param name="url">The URL to download from</param> /// <returns>True if it was successful, false if not</returns> private async Task <bool> DownloadUrl(string url) { //Get the info DownloadInfo info = await Downloader.GetDownloadInfoFromUrl(url); //Validate if (info == null) { return(false); } if (info.VideoCodec == null && info.AudioCodec == null) { return(false); } if (info.RequestedUrls == null && info.Url == null) { return(false); } //Create a new file object string httpVideoUrl = ""; string httpAudioUrl = ""; int uuid = Uuid.GetUuid(); if (!string.IsNullOrEmpty(info.VideoCodec) && info.VideoCodec != "none") { httpVideoUrl = "http://" + MediaserverIp + ":" + MediaserverPort + "/download/" + uuid + "." + info.Extension; } else { httpAudioUrl = "http://" + MediaserverIp + ":" + MediaserverPort + "/download/" + uuid + "." + info.Extension; } PlayableFile newFile = new PlayableFile(httpVideoUrl, httpAudioUrl, info.Title, null, new SubtitleInfo[0], info.Duration, false, FileType.Downloaded); //Add it to the list of files Files.Add(newFile); //Send available files updated message WebServer.SendRawData("AvailableFilesUpdated", new byte[0]); //Download the file to the temporary folder bool isSuccess = await Downloader.DownloadFileFromUrl(url, DownloadFilesPath + "\\incomplete\\" + uuid + "." + info.Extension); //If it was a success, change the availability to true, and add to the http server if (isSuccess) { File.Move(DownloadFilesPath + "\\incomplete\\" + uuid + "." + info.Extension, DownloadFilesPath + "\\" + uuid + "." + info.Extension); //SetHttpServerFiles(); } //If it wasn't a success, remove it from the list of files else { Files.Remove(newFile); newFile.IsAvailable = true; WebServer.SendRawData("AvailableFilesUpdated", new byte[0]); } //Send available files updated message //WebServer.SendRawData("AvailableFilesUpdated", new byte[0]); //Return whether it was success return(isSuccess); }
/// <summary> /// Rescans the media file directory and updates the files available for download /// </summary> private bool RescanForFiles() { ConLog.Log("HTTP Server", "Rescanning media and download folders for available files", LogType.Info); bool hasAnythingChanged = false; string[] filesFound = Directory.GetFiles(MediaFilesPath, "*.*", SearchOption.TopDirectoryOnly); //Repopulate playable file array List <PlayableFile> newMediaFiles = new List <PlayableFile>(); //Add back in all the downloads that are still there foreach (PlayableFile file in Files) { if (file.Type == FileType.Downloaded) { if (File.Exists(DownloadFilesPath + "\\" + Path.GetFileName(file.VideoUrl)) || File.Exists(DownloadFilesPath + "\\" + Path.GetFileName(file.AudioUrl))) { newMediaFiles.Add(new PlayableFile(file.VideoUrl, file.AudioUrl, file.Title, file.Sha1, file.Subtitles, file.Duration, true, FileType.Downloaded)); if (!file.IsAvailable) { hasAnythingChanged = true; file.IsAvailable = true; } } else if (!file.IsAvailable) { newMediaFiles.Add(file); } else { hasAnythingChanged = true; ConLog.Log("HTTP Server", "Download titled " + file.Title + " was removed because the file was deleted", LogType.Warning); } } } //Add the media foreach (string file in filesFound) { //Check if it's audio or video MediaInfoWrapper wrapper = new MediaInfoWrapper(file); //Check that we have media if (wrapper.AudioStreams.Count == 0 && wrapper.VideoStreams.Count == 0) { continue; } string fileName = Path.GetFileName(file); //We have media. Check if it's audio or video if (wrapper.HasVideo) { newMediaFiles.Add(new PlayableFile("http://" + MediaserverIp + ":" + MediaserverPort + "/media/" + fileName, "", fileName, null, new SubtitleInfo[0], wrapper.Duration / 1000.0, true, FileType.Offline)); } else { newMediaFiles.Add(new PlayableFile("", "http://" + MediaserverIp + ":" + MediaserverPort + "/media/" + fileName, fileName, null, new SubtitleInfo[0], wrapper.Duration / 1000.0, true, FileType.Offline)); } } //Check if this differs from the old one foreach (PlayableFile file in newMediaFiles) { PlayableFile matchFound = null; foreach (PlayableFile possibleMatch in Files) { if (file.VideoUrl == possibleMatch.VideoUrl && file.AudioUrl == possibleMatch.AudioUrl && file.Title == possibleMatch.Title && file.Duration == possibleMatch.Duration && file.IsAvailable == possibleMatch.IsAvailable) { matchFound = possibleMatch; } } //If we didn't find a match, something has changed if (matchFound == null) { hasAnythingChanged = true; //If it's not a download, generate a sha1 hash if (file.Type == FileType.Offline) { //Get the file path string fileName; if (!string.IsNullOrEmpty(file.VideoUrl)) { fileName = Path.GetFileName(file.VideoUrl); } else { fileName = Path.GetFileName(file.AudioUrl); } string filePath = MediaFilesPath + "/" + fileName; file.Sha1 = GenerateSha1OfFile(filePath); } } //If we did find a match, copy the sha1 hash else { file.Sha1 = matchFound.Sha1; } } if (newMediaFiles.Count != Files.Count) { hasAnythingChanged = true; } Files = newMediaFiles; //Set the HTTP server's arrays SetHttpServerFiles(); if (hasAnythingChanged) { ConLog.Log("HTTP Server", "Scan complete and the available files has changed. Extracting subtitles and fonts from all media files", LogType.Ok); ExtractAllSubtitlesAndFontsForAllMediaFiles(); ConLog.Log("HTTP Server", "Subtitles and fonts extracted", LogType.Ok); } else { ConLog.Log("HTTP Server", "Scan complete and the available files has not changed", LogType.Ok); } return(hasAnythingChanged); }