private string DownloadImage(string url, string objectid) { var result = HttpUtil.DownloadImage(url, out var contentType); var stream = StreamUtil.BytesToStream(result); var hash_code = SHAUtil.GetFileSHA1(stream); var config = StoreConfig.Config; var id = Guid.NewGuid().ToString(); var fileName = $"{id}.{url.Substring(url.LastIndexOf("/") + 1).GetFileType()}"; ServiceContainer.Resolve <IStoreStrategy>(config?.Type).Upload(stream, fileName, out var filePath); var data = new sys_file() { sys_fileId = id, name = fileName, real_name = fileName, hash_code = hash_code, file_type = "gallery", content_type = contentType, objectId = objectid }; return(Broker.Create(data)); }
public string InsertUseTransactionScope(bool success) { //插入数据,使用数据库事务,多连接时会命名用到MSDTC using (TransactionScope trans = new TransactionScope()) { try { var service1 = IoCHelper.Resolve <ICRUDService <sys_log> >(); sys_log e1 = new sys_log() { Id = GuidHelper.NewGuid().ToString(), CreateDate = DateTime.Now }; int i1 = service1.Insert(e1); var service2 = IoCHelper.Resolve <ICRUDService <sys_file> >(); string id = success ? GuidHelper.NewGuid().ToString() : GuidHelper.NewGuid().ToString().PadRight(100, '0'); sys_file e2 = new sys_file() { Id = id, CreateDate = DateTime.Now }; int i2 = service2.Insert(e2); trans.Complete(); return($"成功{i1},{i2}"); } catch (Exception) { Transaction.Current.Rollback(); throw; } } }
public string InsertUseDbContextTransaction(bool success) { //插入数据,使用数据库事务,不支持多连接。 var dbContext = IoCHelper.Resolve <IDbContextCore>(); using (IDbContextTransaction trans = dbContext.BeginTransaction()) { try { var service1 = IoCHelper.Resolve <ICRUDService <sys_log> >(); service1.Use(dbContext); sys_log e1 = new sys_log() { Id = GuidHelper.NewGuid().ToString(), CreateDate = DateTime.Now }; int i1 = service1.Insert(e1); var service2 = IoCHelper.Resolve <ICRUDService <sys_file> >(); service2.Use(dbContext); string id = success ? GuidHelper.NewGuid().ToString() : GuidHelper.NewGuid().ToString().PadRight(100, '0'); sys_file e2 = new sys_file() { Id = id, CreateDate = DateTime.Now }; int i2 = service2.Insert(e2); trans.Commit(); return($"成功{i1},{i2}"); } catch (Exception) { trans.Rollback(); throw; } } }
public string Insert(bool success) { //插入数据 var service1 = IoCHelper.Resolve <ICRUDService <sys_log> >(); sys_log e1 = new sys_log() { Id = GuidHelper.NewGuid().ToString(), CreateDate = DateTime.Now }; int i1 = service1.Insert(e1); var service2 = IoCHelper.Resolve <ICRUDService <sys_file> >(); string id = success ? GuidHelper.NewGuid().ToString() : GuidHelper.NewGuid().ToString().PadRight(100, '0'); sys_file e2 = new sys_file() { Id = id, CreateDate = DateTime.Now }; int i2 = service2.Insert(e2); return($"成功{i1},{i2}"); }
public void Execute(PersistBrokerPluginContext context) { var entity = context.Entity as wechat_material; switch (context.Action) { case EntityAction.PreCreate: case EntityAction.PreUpdate: // 如果素材未上传到系统,则根据url请求图片保存 if (string.IsNullOrEmpty(entity.sys_fileid)) { var result = HttpUtil.DownloadImage(entity.url, out var contentType); var id = Guid.NewGuid().ToString(); var stream = StreamUtil.BytesToStream(result); var hash_code = SHAUtil.GetFileSHA1(stream); var config = StoreConfig.Config; ServiceContainer.Resolve<IStoreStrategy>(config?.Type).Upload(stream, entity.name, out var filePath); var sysImage = new sys_file() { sys_fileId = id, name = entity.name, real_name = entity.name, hash_code = hash_code, file_type = "wechat_material", content_type = contentType, objectId = entity.Id }; new SysFileService(context.Broker).CreateData(sysImage); entity.sys_fileid = id; entity.local_url = SysFileService.GetDownloadUrl(id); } break; case EntityAction.PreDelete: WeChatApi.DeleteMaterial(entity.GetAttributeValue<string>("media_id")); context.Broker.Delete("sys_file", entity.GetAttributeValue<string>("sys_fileid")); break; default: break; } }
public ActionResult Show(FileSerial obj) { //string fileIdText = obj.GetValue("fileId").ToString(); //string serialText = obj.GetValue("serial").ToString(); //int fileId = 0, serial = 0; //int.TryParse(fileIdText, out fileId); //int.TryParse(serialText, out serial); var fileId = obj.FileId; var serial = obj.Serial; if (fileId > 0) { using (var db = new SysContext()) { sys_file file = null; if (serial > 0) { file = db.sys_file.Find(fileId, serial); } else { file = db.sys_file.FirstOrDefault(f => f.FileId == fileId); } if (file != null) { var path = Server.MapPath("~" + file.Path); if (System.IO.File.Exists(path)) { //return File(path, MimeMapping.GetMimeMapping(path), file.FileName); return(File(path, MimeMapping.GetMimeMapping(path))); } } } } return(HttpNotFound()); }