public async ValueTask <IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null) { using (var stream = new MemoryStream(data, 0, data.Length)) { return(await SaveAsync(stream, file, contentType, overwritePolicy)); } }
public async ValueTask <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null) { var uploadBlob = true; var blockBlob = _container.Value.GetBlockBlobReference(file.Path); var blobExists = await blockBlob.ExistsAsync(); if (blobExists) { if (overwritePolicy == OverwritePolicy.Never) { throw new FileAlreadyExistsException(Name, file.Path); } await blockBlob.FetchAttributesAsync(); if (overwritePolicy == OverwritePolicy.IfContentModified) { using (var md5 = MD5.Create()) { data.Seek(0, SeekOrigin.Begin); var contentMd5 = Convert.ToBase64String(md5.ComputeHash(data)); data.Seek(0, SeekOrigin.Begin); uploadBlob = contentMd5 != blockBlob.Properties.ContentMD5; } } } if (metadata != null) { foreach (var kvp in metadata) { blockBlob.Metadata.Add(kvp.Key, kvp.Value); } } if (uploadBlob) { await blockBlob.UploadFromStreamAsync(data); } var reference = new AzureFileReference(blockBlob, true); if (reference.Properties.ContentType == contentType) { return(reference); } reference.Properties.ContentType = contentType; await reference.SavePropertiesAsync(); return(reference); }
public async ValueTask <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null) { var fileReference = await InternalGetAsync(file, withMetadata : true, checkIfExists : false); var fileExists = File.Exists(fileReference.FileSystemPath); if (fileExists) { if (overwritePolicy == OverwritePolicy.Never) { throw new FileAlreadyExistsException(Name, file.Path); } } var properties = fileReference.Properties as FileSystemFileProperties; var(ETag, ContentMd5) = ComputeHashes(data); if (!fileExists || overwritePolicy == OverwritePolicy.Always || (overwritePolicy == OverwritePolicy.IfContentModified && properties.ContentMd5 != ContentMd5)) { EnsurePathExists(fileReference.FileSystemPath); using (var fileStream = File.Open(fileReference.FileSystemPath, FileMode.Create, FileAccess.Write)) { await data.CopyToAsync(fileStream); } } properties.ContentType = contentType; properties.ExtendedProperties.ETag = ETag; properties.ExtendedProperties.ContentMd5 = ContentMd5; if (metadata != null) { foreach (var kvp in metadata) { properties.Metadata.Add(kvp.Key, kvp.Value); } } await fileReference.SavePropertiesAsync(); return(fileReference); }
public static ValueTask <IFileReference> SaveAsync(this IAbpStore store, Stream data, string path, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null) { return(store.SaveAsync(data, new PrivateFileReference(path), contentType, overwritePolicy, metadata)); }
public static ValueTask <IFileReference> SaveAsync(this IStore store, Stream data, string path, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always) => store.SaveAsync(data, new Internal.PrivateFileReference(path), contentType, overwritePolicy);
public ValueTask <IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null) => this.innerStore.SaveAsync(data, file, contentType, overwritePolicy);
public async ValueTask <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always) { var fileReference = await this.InternalGetAsync(file, withMetadata : true, checkIfExists : false); var fileExists = File.Exists(fileReference.FileSystemPath); if (fileExists) { if (overwritePolicy == OverwritePolicy.Never) { throw new Exceptions.FileAlreadyExistsException(this.Name, file.Path); } } var properties = fileReference.Properties as Internal.FileSystemFileProperties; var hashes = ComputeHashes(data); if (!fileExists || overwritePolicy == OverwritePolicy.Always || (overwritePolicy == OverwritePolicy.IfContentModified && properties.ContentMD5 != hashes.ContentMD5)) { this.EnsurePathExists(fileReference.FileSystemPath); using (var fileStream = File.Open(fileReference.FileSystemPath, FileMode.Create, FileAccess.Write)) { await data.CopyToAsync(fileStream); } } properties.ContentType = contentType; properties.ExtendedProperties.ETag = hashes.ETag; properties.ExtendedProperties.ContentMD5 = hashes.ContentMD5; await fileReference.SavePropertiesAsync(); return(fileReference); }
public static ValueTask <IFileReference> SaveAsync(this IStore store, byte[] data, string path, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null) => store.SaveAsync(data, new Internal.PrivateFileReference(path), contentType, overwritePolicy, metadata);
public ValueTask <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null) { return(_innerStore.SaveAsync(data, file, contentType, overwritePolicy)); }
public ValueTask <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always) => this.innerStore.SaveAsync(data, file, contentType, overwritePolicy);
public static async Task <bool> FetchFilesByFileListLink(Uri ftpFileListLink, string targetPath, OverwritePolicy policy, CancellationTokenSource cancelToken, IProgress <ProgressInfo> progress) { if (ftpFileListLink == null || ftpFileListLink.Scheme != Uri.UriSchemeFtp || string.IsNullOrWhiteSpace(targetPath) || policy == OverwritePolicy.AlwaysAsking) { return(false); } try { if (Directory.Exists(targetPath) == false) { Directory.CreateDirectory(targetPath); } FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpFileListLink); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Timeout = 5000; FtpWebResponse response = await request.GetResponseAsync() as FtpWebResponse; // https://docs.microsoft.com/en-us/dotnet/api/system.net.ftpwebresponse.statuscode if (response == null || response.ContentLength <= 0 || response.StatusCode != FtpStatusCode.CommandOK) { progress?.Report(new ProgressInfo { PercentOrErrorCode = 100, Message = Languages.Global.Str0CantFetchRemoteGlossaryFileList, InfoObject = response, }); return(false); } string responseString = null; var stream = response.GetResponseStream(); using (var reader = new StreamReader(stream, Encoding.UTF8)) { responseString = await reader.ReadToEndAsync(); } response.Close(); if (string.IsNullOrWhiteSpace(responseString)) { progress?.Report(new ProgressInfo { PercentOrErrorCode = 100, Message = Languages.Global.Str0NoGlossaryFileCanFetch, }); return(false); } var remoteRelFiles = responseString.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); if (remoteRelFiles == null || remoteRelFiles.Length <= 0) { progress?.Report(new ProgressInfo { PercentOrErrorCode = 100, Message = Languages.Global.Str0NoGlossaryFileCanFetch, }); return(true); } for (int i = 0; i < remoteRelFiles.Length; ++i) { var relFn = remoteRelFiles[i]; try { var locFn = Path.GetFullPath(Path.Combine(targetPath, relFn)); if (Directory.Exists(locFn)) { continue; } Uri relUri = new Uri(ftpFileListLink, relFn); request = (FtpWebRequest)WebRequest.Create(relUri); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Timeout = 5000; response = await request.GetResponseAsync() as FtpWebResponse; if (response == null || response.ContentLength <= 0 || response.StatusCode != FtpStatusCode.CommandOK) { continue; } if (File.Exists(locFn)) { switch (policy) { case OverwritePolicy.Skip: continue; case OverwritePolicy.FileSizeLarger: using (var fileStream = new FileStream(locFn, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (fileStream.Length >= response.ContentLength) { continue; } } break; case OverwritePolicy.FileDateNew: var reqDate = (FtpWebRequest)WebRequest.Create(relUri); reqDate.Method = WebRequestMethods.Ftp.GetDateTimestamp; reqDate.Timeout = 3000; var rspDate = await reqDate.GetResponseAsync() as FtpWebResponse; if (rspDate == null || rspDate.StatusCode != FtpStatusCode.CommandOK) { continue; } var locDate = File.GetLastWriteTimeUtc(locFn); rspDate.Close(); if (rspDate.LastModified.ToUniversalTime() < locDate) { continue; } break; } } using (var fileStream = new FileStream(locFn, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { await response.GetResponseStream().CopyToAsync(fileStream); response.Close(); } progress?.Report(new ProgressInfo { PercentOrErrorCode = (i + 1) * 100 / remoteRelFiles.Length, Message = string.Format(Languages.Global.Str2FetchFilesFractions, i + 1, remoteRelFiles.Length), }); } catch { } } } catch (Exception ex) { progress?.Report(new ProgressInfo { PercentOrErrorCode = -1, Message = ex.Message, InfoObject = ex }); return(false); } progress?.Report(new ProgressInfo { PercentOrErrorCode = 100, Message = Languages.Global.Str0AllGlossaryFileFetched, }); return(true); }
public static async Task <bool> FetchFilesByFileListLink(string fileListLink, string targetPath, OverwritePolicy policy, CancellationTokenSource cancelToken, IProgress <ProgressInfo> progress) { if (string.IsNullOrWhiteSpace(fileListLink) || string.IsNullOrWhiteSpace(targetPath) || policy == OverwritePolicy.AlwaysAsking || policy == OverwritePolicy.FileDateNew) { return(false); } try { Uri uri = new Uri(fileListLink); if (Directory.Exists(targetPath) == false) { Directory.CreateDirectory(targetPath); } // fetch by FTP protocol if (uri.Scheme == Uri.UriSchemeFtp) { return(await FetchFilesByFileListLink(uri, targetPath, policy, cancelToken, progress)); } using (var client = new HttpClient()) { var response = await client.GetAsync(uri); if (response == null || response.IsSuccessStatusCode == false) { progress?.Report(new ProgressInfo { PercentOrErrorCode = -1, Message = Languages.Global.Str0CantFetchRemoteGlossaryFileList, InfoObject = response, }); return(false); } var responseString = await response.Content.ReadAsStringAsync(); if (string.IsNullOrWhiteSpace(responseString)) { progress?.Report(new ProgressInfo { PercentOrErrorCode = 100, Message = Languages.Global.Str0NoGlossaryFileCanFetch, }); return(true); } var remoteRelFiles = responseString.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); if (remoteRelFiles == null || remoteRelFiles.Length <= 0) { progress?.Report(new ProgressInfo { PercentOrErrorCode = 100, Message = Languages.Global.Str0NoGlossaryFileCanFetch, }); return(true); } for (int i = 0; i < remoteRelFiles.Length; ++i) { var relFn = remoteRelFiles[i]; try { var locFn = Path.GetFullPath(Path.Combine(targetPath, relFn)); if (Directory.Exists(locFn)) { goto exit1; } if (Path.HasExtension(locFn) == false) { // this locFn is a directory Directory.CreateDirectory(locFn); goto exit1; } Uri relUri = new Uri(uri, relFn); response = await client.GetAsync(relUri); if (response == null || response.IsSuccessStatusCode == false || response.Content.Headers.ContentLength <= 0) { goto exit1; } if (File.Exists(locFn)) { switch (policy) { case OverwritePolicy.Skip: goto exit1; case OverwritePolicy.FileSizeLarger: using (var fileStream = new FileStream(locFn, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (fileStream.Length >= response.Content.Headers.ContentLength) { goto exit1; } } break; } } using (var stream = new FileStream(locFn, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { await response.Content.CopyToAsync(stream); } exit1: progress?.Report(new ProgressInfo { PercentOrErrorCode = (i + 1) * 100 / remoteRelFiles.Length, Message = string.Format(Languages.Global.Str2FetchFilesFractions, i + 1, remoteRelFiles.Length), }); } catch { } } } } catch (Exception ex) { progress?.Report(new ProgressInfo { PercentOrErrorCode = -1, Message = ex.Message, InfoObject = ex }); return(false); } progress?.Report(new ProgressInfo { PercentOrErrorCode = 100, Message = Languages.Global.Str0AllGlossaryFileFetched, }); return(true); }
static ReturnCode ParseOptions([NotNull] string[] args) { if (args == null) { throw new ArgumentNullException("args"); } string jpegQualityString = null, imageFormatName = null, angleString = null, isoCatModeName = null, regionString = null, threadCountString = null, overwritePolicyString = null; string importerList = MapUtility.GetImporters().JoinToString(c => c.Format.ToString()); bool printHelp = false; opts = new OptionSet() .Add("a=|angle=", "Angle (orientation) from which the map is drawn. May be -90, 0, 90, 180, or 270. Default is 0.", o => angleString = o) .Add("tryhard", "Try ALL the map converters on map files that cannot be loaded normally.", o => p.TryHard = (o != null)) .Add("e=|export=", "Image format to use for exporting. " + "Supported formats: PNG (default), BMP, GIF, JPEG, TIFF.", o => imageFormatName = o) .Add("f=|filter=", "Pattern to filter input file names, e.g. \"*.dat\" or \"builder*\". " + "Applicable only when a directory name is given as input.", o => p.InputFilter = o) .Add("g|nogradient", "Disables altitude-based gradient/shading on terrain.", o => p.NoGradient = (o != null)) .Add("i=|importer=", "Optional: Converter used for importing/loading maps. " + "Available importers: Auto (default), " + importerList, o => importerName = o) .Add("l|seethroughlava", "Makes all lava partially see-through, instead of opaque.", o => p.SeeThroughLava = (o != null)) .Add("m=|mode=", "Rendering mode. May be \"normal\" (default), \"cut\" (cuts out a quarter of the map, revealing inside), " + "\"peeled\" (strips the outer-most layer of blocks), \"chunk\" (renders only a specified region of the map).", o => isoCatModeName = o) .Add("o=|output=", "Path to save images to. " + "If not specified, images will be saved to the maps' directories.", o => p.OutputDirName = o) .Add("q=|quality=", "Sets JPEG compression quality. Between 0 and 100. Default is 80. " + "Applicable only when exporting images to .jpg or .jpeg.", o => jpegQualityString = o) .Add("r|recursive", "Look through all sub-directories for map files. " + "Applicable only when a directory name is given as input.", o => p.Recursive = (o != null)) .Add("t=|threads=", "Number of threads to use, to render multiple files in parallel. Default is CPU count.", o => threadCountString = o) .Add("region=", "Region of the map to render. Should be given in following format: \"region=x1,y1,z1,x2,y2,z2\" " + "Applicable only when rendering mode is set to \"chunk\".", o => regionString = o) .Add("s|noshadows", "Disables rendering of shadows.", o => p.NoShadows = (o != null)) .Add("u|uncropped", "Does not crop the finished map image, leaving some empty space around the edges.", o => p.Uncropped = (o != null)) .Add("w|seethroughwater", "Makes all water see-through, instead of mostly opaque.", o => p.SeeThroughWater = (o != null)) .Add("x|regex", "Enable regular expressions in \"filter\".", o => p.UseRegex = (o != null)) .Add("y|overwrite=", "When to overwrite existing image files: Never, Ask (default), IfNewer, Always", o => overwritePolicyString = o) .Add("?|h|help", "Prints out the options.", o => printHelp = (o != null)); List <string> pathList; try { pathList = opts.Parse(args); } catch (OptionException ex) { Console.Error.Write("MapRenderer: "); Console.Error.WriteLine(ex.Message); PrintHelp(); return(ReturnCode.ArgumentError); } if (printHelp) { PrintHelp(); Environment.Exit((int)ReturnCode.Success); } if (pathList.Count == 0) { Console.Error.WriteLine("MapRenderer: At least one file or directory name required."); PrintUsage(); return(ReturnCode.ArgumentError); } p.InputPathList = pathList.ToArray(); // Parse angle int angle = 0; if (angleString != null && (!Int32.TryParse(angleString, out angle) || angle != -90 && angle != 0 && angle != 180 && angle != 270)) { Console.Error.WriteLine("MapRenderer: Angle must be a number: -90, 0, 90, 180, or 270"); return(ReturnCode.ArgumentError); } p.Angle = angle; // Parse mode IsoCatMode mode = IsoCatMode.Normal; if (isoCatModeName != null && !EnumUtil.TryParse(isoCatModeName, out mode, true)) { Console.Error.WriteLine( "MapRenderer: Rendering mode should be: \"normal\", \"cut\", \"peeled\", or \"chunk\"."); return(ReturnCode.ArgumentError); } p.Mode = mode; // Parse region (if in chunk mode) if (mode == IsoCatMode.Chunk) { if (regionString == null) { Console.Error.WriteLine("MapRenderer: Region parameter is required when mode is set to \"chunk\""); return(ReturnCode.ArgumentError); } try { string[] regionParts = regionString.Split(','); p.Region = new BoundingBox(Int32.Parse(regionParts[0]), Int32.Parse(regionParts[1]), Int32.Parse(regionParts[2]), Int32.Parse(regionParts[3]), Int32.Parse(regionParts[4]), Int32.Parse(regionParts[5])); } catch { Console.Error.WriteLine( "MapRenderer: Region should be specified in the following format: \"--region=x1,y1,z1,x2,y2,z2\""); } } else if (regionString != null) { Console.Error.WriteLine( "MapRenderer: Region parameter is given, but rendering mode was not set to \"chunk\""); } // Parse given image format if (imageFormatName != null) { if (imageFormatName.Equals("BMP", StringComparison.OrdinalIgnoreCase)) { p.ExportFormat = ImageFormat.Bmp; p.ImageFileExtension = ".bmp"; } else if (imageFormatName.Equals("GIF", StringComparison.OrdinalIgnoreCase)) { p.ExportFormat = ImageFormat.Gif; p.ImageFileExtension = ".gif"; } else if (imageFormatName.Equals("JPEG", StringComparison.OrdinalIgnoreCase) || imageFormatName.Equals("JPG", StringComparison.OrdinalIgnoreCase)) { p.ExportFormat = ImageFormat.Jpeg; p.ImageFileExtension = ".jpg"; } else if (imageFormatName.Equals("PNG", StringComparison.OrdinalIgnoreCase)) { p.ExportFormat = ImageFormat.Png; p.ImageFileExtension = ".png"; } else if (imageFormatName.Equals("TIFF", StringComparison.OrdinalIgnoreCase) || imageFormatName.Equals("TIF", StringComparison.OrdinalIgnoreCase)) { p.ExportFormat = ImageFormat.Tiff; p.ImageFileExtension = ".tif"; } else { Console.Error.WriteLine( "MapRenderer: Image file format should be: BMP, GIF, JPEG, PNG, or TIFF"); return(ReturnCode.ArgumentError); } } // Parse JPEG quality if (jpegQualityString != null) { if (p.ExportFormat.Guid == ImageFormat.Jpeg.Guid) { int jpegQuality; if (!Int32.TryParse(jpegQualityString, out jpegQuality) || jpegQuality < 0 || jpegQuality > 100) { Console.Error.WriteLine( "MapRenderer: JpegQuality parameter should be a number between 0 and 100"); return(ReturnCode.ArgumentError); } p.JpegQuality = jpegQuality; } else { Console.Error.WriteLine( "MapRenderer: JpegQuality parameter given, but image export format was not set to \"JPEG\"."); } } if (p.MapImporter != null && p.TryHard) { Console.Error.WriteLine("MapRenderer: --tryhard flag can only be used when importer is \"auto\"."); return(ReturnCode.ArgumentError); } if (p.InputFilter == null && p.UseRegex) { Console.Error.WriteLine("MapRenderer: --regex flag can only be used when --filter is specified."); return(ReturnCode.ArgumentError); } // Parse theread count byte tempThreadCount = 2; if (threadCountString != null && (!Byte.TryParse(threadCountString, out tempThreadCount) || tempThreadCount < 1)) { Console.Error.WriteLine("MapRenderer: --threads flag must be a number between 1 and 255"); return(ReturnCode.ArgumentError); } p.ThreadCount = tempThreadCount; p.OutputDirGiven = (p.OutputDirName != null); // Parse OverwritePolicy OverwritePolicy op = OverwritePolicy.Ask; if (overwritePolicyString != null && !Enum.TryParse(overwritePolicyString, true, out op)) { Console.Error.WriteLine( "MapRenderer: Overwrite mode should be: \"never\", \"ask\", \"ifNewer\", or \"always\"."); return(ReturnCode.ArgumentError); } p.OverwritePolicy = op; return(ReturnCode.Success); }
public async ValueTask <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always) { var uploadBlob = true; var blockBlob = this.container.Value.GetBlockBlobReference(file.Path); var blobExists = await blockBlob.ExistsAsync(); if (blobExists) { if (overwritePolicy == OverwritePolicy.Never) { throw new Exceptions.FileAlreadyExistsException(this.Name, file.Path); } await blockBlob.FetchAttributesAsync(); if (overwritePolicy == OverwritePolicy.IfContentModified) { using (var md5 = MD5.Create()) { data.Seek(0, SeekOrigin.Begin); var contentMD5 = Convert.ToBase64String(md5.ComputeHash(data)); data.Seek(0, SeekOrigin.Begin); uploadBlob = (contentMD5 != blockBlob.Properties.ContentMD5); } } } if (uploadBlob) { await blockBlob.UploadFromStreamAsync(data); } var reference = new Internal.AzureFileReference(blockBlob, withMetadata: true); if (reference.Properties.ContentType != contentType) { reference.Properties.ContentType = contentType; await reference.SavePropertiesAsync(); } return(reference); }
public async ValueTask <IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always) { using (var stream = new SyncMemoryStream(data, 0, data.Length)) { return(await this.SaveAsync(stream, file, contentType, overwritePolicy)); } }