Beispiel #1
0
        /// <summary>
        /// 上传素材(已上传则直接返回)
        /// </summary>
        /// <param name="type"></param>
        /// <param name="fileId"></param>
        /// <returns></returns>
        public WeChatSuccessUploadResponse CreateData(MaterialType type, string fileId)
        {
            var file = new SysFileService().GetData(fileId);

            AssertUtil.CheckBoolean <SpException>(file == null, $"根据fileid:{fileId}未找到记录", "36B5F5C9-ED65-4CAC-BE60-712278056EA9");

            // 检查素材库是否已经上传
            var data = Broker.Retrieve <wechat_material>("select * from wechat_material where sys_fileid = @sys_fileid", new Dictionary <string, object>()
            {
                { "@sys_fileid", file.sys_fileId }
            });

            if (data != null)
            {
                return(new WeChatSuccessUploadResponse()
                {
                    media_id = data.media_id,
                    url = data.url
                });
            }

            // 获取文件流
            var config = StoreConfig.Config;
            var stream = ServiceContainer.Resolve <IStoreStrategy>(config?.Type).GetStream(fileId);
            var media  = WeChatApi.AddMaterial(type, stream, file.name, file.content_type);

            stream.Dispose();

            // 创建素材记录
            var material = new wechat_material()
            {
                wechat_materialId = Guid.NewGuid().ToString(),
                media_id          = media.media_id,
                url        = media.url,
                sys_fileid = fileId,
                local_url  = SysFileService.GetDownloadUrl(fileId),
                name       = file.name,
                type       = type.ToMaterialTypeString()
            };

            CreateData(material);
            return(media);
        }
Beispiel #2
0
 public List <string> UploadImage(Pixabay.ImageModel image)
 {
     return(Broker.ExecuteTransaction(() =>
     {
         var data = new gallery()
         {
             Id = Guid.NewGuid().ToString(),
             tags = image.tags
         };
         data.previewid = DownloadImage(image.previewURL, data.Id);
         data.imageid = DownloadImage(image.largeImageURL, data.Id);
         data.preview_url = SysFileService.GetDownloadUrl(data.previewid);
         data.image_url = SysFileService.GetDownloadUrl(data.imageid);
         base.CreateData(data);
         return new List <string>()
         {
             data.previewid, data.imageid
         };
     }));
 }
        /// <summary>
        /// 创建图文消息素材
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public string CreateData(string fileid)
        {
            var file = Broker.Retrieve <sys_file>(fileid);
            var data = Broker.Retrieve <wechat_news_material>("SELECT * FROM wechat_news_material WHERE fileid = @id", new Dictionary <string, object>()
            {
                { "@id", fileid }
            });

            if (data == null)
            {
                var stream = ServiceContainer.Resolve <IStoreStrategy>(StoreConfig.Config.Type).GetStream(fileid);
                data = new wechat_news_material()
                {
                    Id     = Guid.NewGuid().ToString(),
                    fileid = fileid
                };
                data.media_url = WeChatApi.UploadImg(stream, file.name, file.content_type)?.url;
                data.local_url = SysFileService.GetDownloadUrl(fileid, false);
                CreateData(data);
            }
            return(data.media_url);
        }
Beispiel #4
0
 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;
     }
 }