public static string GetWebFile(string url) { if (string.IsNullOrEmpty(url)) { return(string.Empty); } var cacheFile = Path.Combine(CacheDirectory, GetFileNameFromUrl(url)); lock (cacheLock) { if (File.Exists(cacheFile) && (new FileInfo(cacheFile)).Length != 0) { logger.Debug($"Returning {url} from file cache {cacheFile}."); return(cacheFile); } else { FileSystem.CreateDirectory(CacheDirectory); try { HttpDownloader.DownloadFile(url, cacheFile); return(cacheFile); } catch (WebException e) { if (e.Response == null) { throw; } var response = (HttpWebResponse)e.Response; if (response.StatusCode != HttpStatusCode.NotFound) { throw; } else { return(string.Empty); } } } } }
/// <summary> /// Returns local file path of file. If link, downloads file to Playnite temp. If raw content, saves file to Playnite temp. /// </summary> /// <param name="file"></param> /// <param name="cancelToken"></param> /// <returns></returns> public static string GetLocalFile(this MetadataFile file, CancellationToken cancelToken) { if (!file.HasImageData) { return(null); } if (file.HasContent) { var resultPath = Path.Combine(PlaynitePaths.TempPath, Guid.NewGuid() + Path.GetExtension(file.FileName)); FileSystem.PrepareSaveFile(resultPath); File.WriteAllBytes(resultPath, file.Content); return(resultPath); } else { if (file.Path.IsHttpUrl()) { var extension = Path.GetExtension(new Uri(file.Path).AbsolutePath); var resultPath = Path.Combine(PlaynitePaths.TempPath, Guid.NewGuid() + extension); FileSystem.PrepareSaveFile(resultPath); HttpDownloader.DownloadFile(file.Path, resultPath, cancelToken); if (cancelToken.IsCancellationRequested) { if (File.Exists(resultPath)) { File.Delete(resultPath); } return(null); } else { return(resultPath); } } else { return(file.Path); } } }
private string ProcessMetadataFile(MetadataFile file, string tempFileName) { if (file.HasContent) { var extension = Path.GetExtension(file.FileName); var fileName = tempFileName + extension; var targetPath = Path.Combine(PlaynitePaths.TempPath, fileName); FileSystem.PrepareSaveFile(targetPath); File.WriteAllBytes(targetPath, file.Content); return(targetPath); } else if (!file.OriginalUrl.IsNullOrEmpty()) { if (file.OriginalUrl.IsHttpUrl()) { var extension = Path.GetExtension(new Uri(file.OriginalUrl).AbsolutePath); var fileName = tempFileName + extension; var targetPath = Path.Combine(PlaynitePaths.TempPath, fileName); var progRes = dialogs.ActivateGlobalProgress((a) => HttpDownloader.DownloadFile(file.OriginalUrl, targetPath, a.CancelToken), new GlobalProgressOptions("LOCDownloadingMediaLabel", true)); if (progRes.Result == true && !progRes.Canceled) { return(targetPath); } else { logger.Error(progRes.Error, $"Failed to download {file.OriginalUrl}."); return(null); } } else { return(file.OriginalUrl); } } else { return(null); } }