public HttpWebRequest Create(string uri) { // TODO: choose rational timeout values HttpWebRequest request = HttpRequestHelper.CreateHttpWebRequest(uri, false); _parent.CreateAuthorizationFilter().Invoke(request); request.ContentType = MimeHelper.GetContentType(Path.GetExtension(_filename)); try { request.Headers.Add("Slug", Path.GetFileNameWithoutExtension(_filename)); } catch (ArgumentException) { request.Headers.Add("Slug", "Image"); } request.Method = _method; using (Stream s = request.GetRequestStream()) { using (Stream inS = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { StreamHelper.Transfer(inS, s); } } return(request); }
public void TestMimeDetect() { var mime = MimeHelper.GetContentType("a.docx"); Assert.AreEqual("application/vnd.openxmlformats-officedocument.wordprocessingml.document", mime); mime = MimeHelper.GetContentType("b.pptx"); Assert.AreEqual("application/vnd.openxmlformats-officedocument.presentationml.presentation", mime); }
public ActionResult Get(Guid id) { var scan = _context.Scans.Find(id); if (scan == null) { return(HttpNotFound()); } var path = Path.Combine(FileStorageProvider.GetCommonPathToScans(), scan.ClaimId.ToString(), scan.LocalFileName); Response.AppendHeader("Content-Disposition", "inline; filename=" + scan.OriginalName); return(File(path, MimeHelper.GetContentType(scan.ExtensionWithDot))); }
public ActionResult Preview(Guid id) { var registriesPath = ConfigurationManager.AppSettings["RegistriesDirectory"]; var previewsPath = ConfigurationManager.AppSettings["PreviewsDirectory"]; var scan = _context.Scans.Find(id); if (scan == null) { return(HttpNotFound()); } Response.AppendHeader("Content-Disposition", "inline; filename=" + scan.OriginalName); return(File(GetScanPreviewFilePath(scan), MimeHelper.GetContentType(scan.ExtensionWithDot))); }
public async Task <HttpRequestMessage> Create(string uri) { // TODO: choose rational timeout values var request = HttpRequestHelper.CreateHttpWebRequest(uri, false); await(await _parent.CreateAuthorizationFilter()).Invoke(request); //request.Headers. //request.ContentType = MimeHelper.GetContentType(Path.GetExtension(_filename)); try { request.Headers.Add("Slug", Path.GetFileNameWithoutExtension(_filename)); } catch (ArgumentException) { request.Headers.Add("Slug", "Image"); } request.Method = new HttpMethod(_method); _fileStream.Seek(0, SeekOrigin.Begin); var streamContent = new HttpStreamContent(_fileStream.AsInputStream()); streamContent.Headers.ContentLength = (ulong?)_fileStream.Length; streamContent.Headers.ContentType = new HttpMediaTypeHeaderValue(MimeHelper.GetContentType(Path.GetExtension(_filename))); request.Content = streamContent; //using (Stream s = request.GetRequestStream()) //{ // using (Stream inS = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.Read)) // { // StreamHelper.Transfer(inS, s); // } //} return(request); }
public HttpWebRequest Create(string uri) { // TODO: ETag support required?? // TODO: choose rational timeout values HttpWebRequest request = HttpRequestHelper.CreateHttpWebRequest(uri, false); request.ContentType = MimeHelper.GetContentType(Path.GetExtension(_filename)); if (_parent._options != null && _parent._options.SupportsSlug) { request.Headers.Add("Slug", Path.GetFileNameWithoutExtension(_filename)); } request.Method = _method; request.AllowWriteStreamBuffering = _allowWriteStreamBuffering; if (_etag != null && _etag.Length != 0) { request.Headers.Add("If-match", _etag); } _parent._requestFilter(request); using (Stream inS = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (CancelableStream cs = new CancelableStream(inS)) { request.ContentLength = cs.Length; using (Stream s = request.GetRequestStream()) { StreamHelper.Transfer(cs, s); } } } return(request); }
internal void AddFile(string filePath) { Write("Content-Type: " + MimeHelper.GetContentType(Path.GetExtension(filePath)) + Environment.NewLine, _requestBodyTop); Write("Content-Transfer-Encoding: binary" + Environment.NewLine + Environment.NewLine, _requestBodyTop); Write(Environment.NewLine, _requestBodyBottom); }
public async Task <HttpRequestMessage> Create(string uri) { // TODO: ETag support required?? // TODO: choose rational timeout values var request = HttpRequestHelper.CreateHttpWebRequest(uri, false); //request.AllowWriteStreamBuffering = _allowWriteStreamBuffering; if (_etag != null && _etag.Length != 0) { request.Headers["If-match"] = _etag; } await _parent._requestFilter(request); using (Stream inS = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { var streamContent = new HttpStreamContent(inS.AsInputStream()); streamContent.Headers.ContentType = HttpMediaTypeHeaderValue.Parse(MimeHelper.GetContentType(Path.GetExtension(_filename))); if (_parent._options != null && _parent._options.SupportsSlug) { request.Headers["Slug"] = Path.GetFileNameWithoutExtension(_filename); } request.Method = new HttpMethod(_method); } return(request); }
/// <summary> /// Fetch the web content in the specific url /// </summary> /// <param name="url">Url in which to fetch the content</param> /// <returns>Local url to downloaded content</returns> public async Task <FetchedFile> FetchAsync(string url) { var uri = new Uri(url); if (uri.Scheme != "http" && uri.Scheme != "https") { throw new NotSupportedException("Not supported Uri Scheme: " + uri.Scheme); } // Target path to save downloaded web file var filePath = Path.Combine(mConfig.FetchDirectory, UrlHelper.UrlToFileName(url)); var client = mConfig.NotUseProxyUrlRegex.IsMatch(url) ? mClientWithoutProxy : mClientWithProxy; var response = await client.GetAsync(url); var statusCode = response.StatusCode; var contentType = response.Content.Headers.ContentType; if (statusCode != HttpStatusCode.Accepted && statusCode != HttpStatusCode.OK) { mLogger.Log(nameof(SimpleFetcher), "Status: " + statusCode + " " + url); throw new IOException(statusCode.ToString()); } if (mConfig.UsePhantomJS && (contentType == null || contentType.MediaType == "text/html")) { // If config is set to use PhantomJS and web content type is HTML, // use PhantomJS to fetch real web page content File.WriteAllText(filePath, FetchPageContentByPhantomJS(url, mConfig.PhantomJSDriverPath)); } else { // Otherwise, directly save it if supported by parser if (contentType == null || UniversalFileParser.IsSupported(contentType.MediaType)) { using (Stream contentStream = await response.Content.ReadAsStreamAsync(), stream = File.Create(filePath)) { await contentStream.CopyToAsync(stream); } } else { throw new NotSupportedException("Not supported media type: " + contentType?.MediaType); } } #region Detect Content MIME Type var detectedContentType = MimeHelper.GetContentType(filePath); if ((detectedContentType == null || detectedContentType == "application/octet-stream" || detectedContentType == "inode/x-empty") && contentType != null) { detectedContentType = contentType.MediaType; } if (detectedContentType == null) { File.Delete(filePath); mLogger.Log(nameof(SimpleFetcher), "Unknown MIME type: " + url); return(null); } if (!UniversalFileParser.IsSupported(detectedContentType)) { File.Delete(filePath); mLogger.Log(nameof(SimpleFetcher), $"Deleted Not-Supported MIME type ({detectedContentType}): {url}"); return(null); } #endregion var detectedCharset = DetectEncoding(filePath, contentType.CharSet); return(new FetchedFile { Url = url, FilePath = filePath, Charset = detectedCharset, MimeType = detectedContentType, FileHash = HashHelper.GetFileMd5(filePath), }); }