using System; using System.IO; using SkiaSharp; using UMC.Web; namespace UMC.ITME.Activities { [Apiumc("System", "Picture", Desc = "上传图片", Weight = 0)] class SitePictureActivity : WebActivity { public override void ProcessActivity(WebRequest request, WebResponse response) { var user = this.Context.Token.Identity(); var key = this.AsyncDialog("Key", "Event"); var imageKey = this.AsyncDialog("id", d => { return this.DialogValue(this.Context.Token.UserId.ToString()); }); var size = this.AsyncDialog("size", "auto"); var seq = this.AsyncDialog("seq", "1"); var webr = UMC.Data.WebResource.Instance(); var media_id = this.AsyncDialog("media_id", g => { if (request.IsApp) { return Web.UIDialog.Create("File"); } else { var from = new Web.UIFormDialog() { Title = "图片上传" }; from.AddFile("选择图片", "media_id", String.Empty); if (String.Equals("auto", size) == false) { from.AddFooter($"图片要求: {size}"); } from.Submit("确认上传", $"{request.Model}.{request.Command}", "UI.Event"); return from; } }); var type = this.AsyncDialog("type", "none"); if (media_id.StartsWith("http://") == false && media_id.StartsWith("https://") == false) { media_id = Uri.UnescapeDataString(media_id); var file = UMC.Data.Reflection.ConfigPath(String.Format("Static\\TEMP\\{0}", media_id.Substring(media_id.IndexOf('/', 2) + 1))); if (System.IO.File.Exists(file)) { media_id = $"file://{file}"; } else { this.Prompt("未有上传的图片"); } } var url = new Uri(media_id); if (String.Equals("auto", size) == false) { System.IO.Stream stream; SKCodec sKCodec = null; SKBitmap src = null; if (media_id.StartsWith("file://")) { stream = File.OpenRead(media_id.Substring(7)); } else { stream = new System.Net.Http.HttpClient().GetStreamAsync(media_id).Result; } try { sKCodec = SKCodec.Create(stream); src = SKBitmap.Decode(sKCodec); if (size.Contains("*") || size.Contains("X") || size.Contains("x")) { var s = size.Split('*', 'x', 'X'); if (src.Width != Utility.IntParse(s[0].Trim(), 0) || src.Height != Utility.IntParse(s[1].Trim(), 0)) { this.Prompt($"图片要求尺寸为{size}"); } } else { var s = size.Split(':'); if (src.Width * Utility.IntParse(s[1].Trim(), 1) != src.Height * Utility.IntParse(s[0].Trim(), 1)) { this.Prompt($"图片要求尺寸为{size}"); } } } finally { stream?.Dispose(); sKCodec?.Dispose(); src?.Dispose(); } } // var url = new Uri(media_id); if (type == "none") { webr.Transfer(url, imageKey, seq); } else { webr.Transfer(url, imageKey, seq, type.ToLower()); } if (String.Equals(key, "Event")) { this.Context.Send(true); } else { this.Context.Send(new UMC.Web.WebMeta().UIEvent(key, this.AsyncDialog("UI", "none"), new ListItem() { Value = seq.ToString(), Text = seq.ToString() }), true); } } } }