Exemple #1
0
        public static bool ValidateUrl(string path, ref string filename)
        {
            Uri uri;

            try {
                uri = new Uri(path);
            } catch (Exception ex) {
                Debug.WriteLine(ex.Message);
                return(false);
            }

            if (uri.IsFile)
            {
                return(File.Exists(uri.LocalPath));
            }

            var client = new HttpClient()
            {
                Timeout = new TimeSpan(0, 0, 10)
            };

            try {
                HttpResponseMessage resTask = AsyncHelper.RunSync(() => client.SendAsync(new HttpRequestMessage(HttpMethod.Head, uri)));
                if (string.IsNullOrEmpty(filename))
                {
                    try {
                        if (resTask.Content.Headers.ContentDisposition != null)
                        {
                            filename = resTask.Content.Headers.ContentDisposition.FileName.Replace("\"", "");
                        }
                        else
                        {
                            filename = Path.GetFileName(uri.LocalPath);
                        }
                    } catch (Exception e) {
                        filename = RandomIdGenerator.GetRandomId(1, 15);
                        Console.WriteLine($"could not get filename from HEAD request. going with randomly generated '{filename}'. error: {e}");
                    }
                }
                else
                {
                    // go with filename provided
                }
                return(true);                // resTask.IsSuccessStatusCode; -> urls from our S3 downloads don't accept HEAD requests, but they do accept GET requests. that sucks.
            } catch (TaskCanceledException ex) {
                Console.WriteLine(ex.Message);
                Console.WriteLine("resource at {0} not reachable", uri);
                return(false);
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                Console.WriteLine("resource at {0} not reachable or other error", uri);
                return(false);
            }
        }
Exemple #2
0
 public string GetTempDir()
 {
     return(Path.Combine(GetUploadsDir(), RandomIdGenerator.GetRandomId(3, 16)));
 }