/// <summary> /// 保存文件 /// </summary> /// <param name="fileInfo"></param> /// <param name="stream"></param> /// <param name="extraArgs"></param> /// <returns></returns> public override async Task <FileMetaInfo> SaveAsync(FileMetaInfo fileInfo, Stream stream, IDictionary <string, object> extraArgs) { if (extraArgs == null || !extraArgs.ContainsKey("context")) { throw new ArgumentException("extraArgs 参数需要context键值"); } var context = extraArgs["context"].ToString(); var rule = FileStoreRuleManager.GetRuleByName(context) as ImageStoreRule; if (rule == null) { throw new Exception("未找到指定规则或默认规则:" + context); } fileInfo.Id = Guid.NewGuid().ToString(); var name = GetInnerFileName(fileInfo); var dir = rule.GetSaveDir(); await SaveFileAsync(dir, name, stream); fileInfo.Path = Path.Combine(rule.GetSaveDir(), name).Replace("\\", "/"); //保存文件信息 await MetaInfoStore.SaveAsync(fileInfo, rule.Id); return(fileInfo); }
private async Task SaveInternalAsync(FileMetaInfo fileInfo, Stream stream, IDictionary <string, object> extraArgs, ImageStoreRule rule, bool modify = false) { if (!fileInfo.ContentType.ToLower().StartsWith("image")) { throw new NotSupportedException("传入文件类型不是有效的图片类型"); } var needSaveFiles = new Dictionary <string, Stream>(); var name = GetInnerFileName(fileInfo); var savedir = modify ? fileInfo.DirPath : rule.GetSaveDir(); //保存原始图片 if (rule.SaveOriginalImage) { var dir = savedir; var svname = rule.AddWaterMarker ? GetInnerFileName(fileInfo, "OriginalImage") : name; var fileStream = stream; await SaveFileAsync(dir, svname, fileStream, rule.AddWaterMarker); if (!rule.ThumbnailSizes.Any()) { stream.Position = 0; needSaveFiles.Add(name, stream); } } //生成缩略图 if (rule.ThumbnailSizes.Any()) { foreach (var thumbConfig in rule.ThumbnailSizes.OrderBy(x => x.Value.Size.Height * x.Value.Size.Width)) { var key = thumbConfig.Key; var config = thumbConfig.Value; var thumbStream = GenThumbnail(stream, config); var thumbName = GetInnerFileName(fileInfo, key); needSaveFiles.Add(thumbName, thumbStream); stream.Position = 0; } //org comp if (rule.SaveOriginalImage && rule.AddWaterMarker && needSaveFiles.Any()) { var orgInstead = needSaveFiles.Last(); var dir = savedir; var stm = await AddWaterMarkAsync(orgInstead.Value); await SaveFileAsync(dir, name, stm); } } //添加水印 if (rule.AddWaterMarker) { foreach (var images in needSaveFiles.ToDictionary(x => x.Key, x => x.Value)) { needSaveFiles[images.Key] = await AddWaterMarkAsync(images.Value); } } //持久化 foreach (var images in needSaveFiles) { var dir = savedir; var svname = images.Key; var fileStream = images.Value; await SaveFileAsync(dir, svname, fileStream); if (fileStream != stream) { fileStream.Dispose(); } } if (extraArgs.ContainsKey("suffix")) { fileInfo.Path = Path.Combine(savedir, GetInnerFileName(fileInfo, extraArgs["suffix"].ToString())) .Replace("\\", "/"); } else if (!rule.SaveOriginalImage && rule.ThumbnailSizes.ContainsKey("org")) { fileInfo.Path = Path.Combine(savedir, GetInnerFileName(fileInfo, "org")).Replace("\\", "/"); } else { fileInfo.Path = Path.Combine(savedir, name).Replace("\\", "/"); } //保存文件信息 await MetaInfoStore.SaveAsync(fileInfo, rule.Id); }