public string UploadFileFromUrl(string fileUrl, FileContentTypeValidator validator) { if (!string.IsNullOrEmpty(fileUrl)) { string fileName = null; string contentType = null; byte[] buffer = null; if (fileUrl.StartsWith("data:", StringComparison.OrdinalIgnoreCase)) // data:[<media type[;charset=utf-8]>][;base64],<data> { string[] parts = fileUrl.Split(','); string[] parts2 = parts[0].Split(':')[1].Split(';'); if (Array.IndexOf <string>(parts2, "base64") > -1) { buffer = Convert.FromBase64String(parts[1]); } else if (parts.Length > 1) { string data = parts[1]; if (!string.IsNullOrEmpty(data)) { string charset = Array.Find <string>(parts2, x => x.StartsWith("charset=", StringComparison.OrdinalIgnoreCase)); if (!string.IsNullOrEmpty(charset)) { charset = charset.Split('=')[1]; if (!string.IsNullOrEmpty(charset)) { try { Encoding encoding = Encoding.GetEncoding(charset); data = HttpUtility.UrlDecode(data, encoding); } catch (ArgumentException) { } } } } buffer = Encoding.UTF8.GetBytes(data); } contentType = parts2[0]; fileName = Guid.NewGuid().ToString("N").Substring(0, 12) + MimeType.GetExtension(contentType); } else if (fileUrl.StartsWith(Uri.UriSchemeHttp + Uri.SchemeDelimiter, StringComparison.OrdinalIgnoreCase) || fileUrl.StartsWith(Uri.UriSchemeHttps + Uri.SchemeDelimiter, StringComparison.OrdinalIgnoreCase)) { fileName = Path.GetFileName(fileUrl.Split('?')[0]); contentType = MimeType.GetMimeType(fileName); string responseContentType = null; using (WebClient webClient = new WebClient()) { buffer = webClient.DownloadData(fileUrl); if (webClient.ResponseHeaders != null) { responseContentType = webClient.ResponseHeaders["Content-Type"]; } } if (!string.IsNullOrEmpty(responseContentType) && string.Compare(responseContentType, contentType, StringComparison.OrdinalIgnoreCase) != 0) { fileName = Path.GetFileNameWithoutExtension(fileName) + MimeType.GetExtension(responseContentType); contentType = responseContentType; } } if (buffer != null) { if (validator != null) { if (!validator.Invoke(contentType)) { throw new InvalidDataException(string.Format(CultureInfo.InvariantCulture, Resources.FileManager_InvalidContentType, contentType)); } } return(UploadFile(fileName, contentType, buffer)); } } return(null); }
private void CopyTemporaryFiles(string directoryName, bool deleteTemporaryFiles) { var temporaryBlobItems = GetBlockBlobs(TemporaryContainer, directoryName + "/"); foreach (var tempBlobItem in temporaryBlobItems) { string fileName = GetNameFromFileId(tempBlobItem.Name); string blobName = GetFileId(fileName); var blob = Container.GetBlobClient(blobName); var tempBlob = TemporaryContainer.GetBlobClient(tempBlobItem.Name); if (MimeType.IsInGroups(tempBlobItem.Properties.ContentType, MimeTypeGroups.Image)) { Stream source = null; try { var downloadResult = tempBlob.DownloadContent().Value; source = downloadResult.Content.ToStream(); byte[] bytes = RotateFlipImageByOrientation(tempBlobItem.Properties.ContentType, source); if (bytes == null) { bytes = downloadResult.Content.ToArray(); } if (bytes != null) { var uploadOptions = new BlobUploadOptions { HttpHeaders = new BlobHttpHeaders { ContentType = tempBlobItem.Properties.ContentType, CacheControl = Settings.ClientCacheControl } }; blob.Upload(new BinaryData(bytes), uploadOptions); } } finally { source?.Dispose(); } DeleteThumbnails(blobName); } else { if (tempBlob.Exists()) { blob.StartCopyFromUri(tempBlob.Uri); } } if (deleteTemporaryFiles) { tempBlob.DeleteIfExists(); } } }