Exemplo n.º 1
0
        private MaterialImage getPictureBoxImage(PictureBox pb, ComboBox imgType)
        {
            if (pb.Image == null)
            {
                return(null);
            }
            MaterialImage mi = new MaterialImage();

            mi.role       = Combo2Role(imgType);;
            mi.sourcePath = pb.ImageLocation;
            return(mi);
        }
Exemplo n.º 2
0
        public async Task <ActionResult <MaterialImage> > Create([FromQuery] CreateRequest request, [FromForm] IFormFile file)
        {
            if (!await _authManager.HasSitePermissionsAsync(request.SiteId,
                                                            MenuUtils.SitePermissions.MaterialImage))
            {
                return(Unauthorized());
            }

            if (file == null)
            {
                return(this.Error(Constants.ErrorUpload));
            }

            var fileName = Path.GetFileName(file.FileName);
            var extName  = PathUtils.GetExtension(fileName);
            var site     = await _siteRepository.GetAsync(request.SiteId);

            if (!_pathManager.IsImageExtensionAllowed(site, extName))
            {
                return(this.Error(Constants.ErrorImageExtensionAllowed));
            }
            if (!_pathManager.IsImageSizeAllowed(site, file.Length))
            {
                return(this.Error(Constants.ErrorImageSizeAllowed));
            }

            var materialFileName     = PathUtils.GetMaterialFileName(fileName);
            var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Image);

            var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath);
            var filePath      = PathUtils.Combine(directoryPath, materialFileName);

            await _pathManager.UploadAsync(file, filePath);

            await _pathManager.AddWaterMarkAsync(site, filePath);

            var image = new MaterialImage
            {
                GroupId = request.GroupId,
                Title   = fileName,
                Url     = PageUtils.Combine(virtualDirectoryPath, materialFileName)
            };

            await _materialImageRepository.InsertAsync(image);

            return(image);
        }
Exemplo n.º 3
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (tbBasePath.Text.Length == 0)
            {
                MessageBox.Show("Basepath not set", "Please enter basepath before generating the material. Example basepath: 'C:/GAMES/Qio/game/baseqio/'",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation,
                                MessageBoxDefaultButton.Button1);
                return;
            }
            if (!Directory.Exists(tbBasePath.Text))
            {
                MessageBox.Show("Invalid basepath", "Basepath directory does not exist. Did you enter a valid path?",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation,
                                MessageBoxDefaultButton.Button1);
                return;
            }
            if (ms.materialExists(tbMatName.Text))
            {
                MessageBox.Show("Name already used", "Material with given name already exists. Please use different name.",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation,
                                MessageBoxDefaultButton.Button1);
                return;
            }
            // for material name like "textures/testGen/testMat123"
            // we want to create:
            // 1. "textures/" dir if not exists
            // 2. "textures/testgen/" dir if not exists
            // 3. textures/testgen/testMat123.png (diffuse map)
            // 4. textures/testgen/testMat123_n.png (normal map)
            // 5. textures/testgen/testMat123_s.png (specular map)
            // 6. textures/testgen/testMat123_h.png (height map)
            // 7. material text inside .mtr file

            string mtrFile = getCurrentMatFileNamePath();


            backupMaterialFile(mtrFile);

            // create path for images if not exist
            string matDir   = FilePath2DirPath(tbMatName.Text);
            string fullPath = MergePaths(tbBasePath.Text, matDir);

            CreateDirs(fullPath);
            // copy and rename images
            List <MaterialImage> images = new List <MaterialImage>();
            MaterialImage        img    = getPictureBoxImage(pictureBox1, cbType1);

            if (img != null)
            {
                images.Add(img);
            }
            img = getPictureBoxImage(pictureBox2, cbType2);
            if (img != null)
            {
                images.Add(img);
            }
            img = getPictureBoxImage(pictureBox3, cbType3);
            if (img != null)
            {
                images.Add(img);
            }
            //   img = getPictureBox3Image();
            // if (img != null)
            {
                //   images.Add(img);
            }

            // convert or copy images
            foreach (MaterialImage mi in images)
            {
                String suffix;
                switch (mi.role)
                {
                case MaterialImageRole.DIFFUSE:
                {
                    suffix = "";
                }
                break;

                case MaterialImageRole.NORMAL:
                {
                    suffix = "_n";
                }
                break;

                case MaterialImageRole.HEIGHT:
                {
                    suffix = "_h";
                }
                break;

                case MaterialImageRole.SPECULAR:
                {
                    suffix = "_s";
                }
                break;

                default:
                {
                    suffix = "_error";       // TODO
                }
                break;
                }
                string ext = Path.GetExtension(mi.sourcePath);
                mi.targetPath = MergePaths(tbBasePath.Text, tbMatName.Text) + suffix + ext;
                if (IsURL(mi.sourcePath))
                {
                    try
                    {
                        using (WebClient wc = new WebClient())
                            wc.DownloadFile(mi.sourcePath, mi.targetPath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Download failed.", "Failed to download '" + mi.sourcePath + "' to '" + mi.targetPath + "'. Check your web connection.",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation,
                                        MessageBoxDefaultButton.Button1);
                        return; // abort
                    }
                }
                else
                {
                    try
                    {
                        File.Copy(mi.sourcePath, mi.targetPath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Failed to copy '" + mi.sourcePath + "' to '" + mi.targetPath + "'. Exception text: " + ex.ToString());
                        return; // abort
                    }
                }
            }



            MaterialImage diffuseMap  = findImageOfRole(images, MaterialImageRole.DIFFUSE);
            MaterialImage specularMap = findImageOfRole(images, MaterialImageRole.SPECULAR);
            MaterialImage normalMap   = findImageOfRole(images, MaterialImageRole.NORMAL);
            MaterialImage heightMap   = findImageOfRole(images, MaterialImageRole.HEIGHT);

            String dateStr = getCurDateTimeStringForFileName();

            string mtrText = tbMatName.Text + Environment.NewLine;

            mtrText += "{" + Environment.NewLine;
            mtrText += "\t// generated on " + dateStr + Environment.NewLine;
            if (diffuseMap != null)
            {
                mtrText += "\tqer_editorImage " + getLocalPath(diffuseMap.targetPath) + Environment.NewLine;
                mtrText += "\tdiffuseMap " + getLocalPath(diffuseMap.targetPath) + Environment.NewLine;
            }
            if (specularMap != null)
            {
                mtrText += "\tspecularMap " + getLocalPath(specularMap.targetPath) + Environment.NewLine;
            }
            if (specularMap != null)
            {
                mtrText += "\tnormalMap " + getLocalPath(normalMap.targetPath) + Environment.NewLine;
            }
            if (heightMap != null)
            {
                mtrText += "\theightMap " + getLocalPath(heightMap.targetPath) + Environment.NewLine;
            }
            mtrText += "}" + Environment.NewLine;

            // append text
            using (StreamWriter sw = File.AppendText(mtrFile))
            {
                sw.Write(mtrText);
            }
            //MessageBox.Show("Generated text " + mtrText);



            MessageBox.Show("Material creation success.", "Successfully added new material text to " + mtrFile + ". Generated text lenght: " + mtrText.Length + ".");

            ms.loadOrReloadMaterialFile(mtrFile);

            sendCommandToGame("mat_refreshMaterialSourceFile " + getCurrentMatFileComboText());
            sendCommandToGame("cg_testMaterial " + tbMatName.Text);
        }
        public async Task <ActionResult <UploadResult> > Upload([FromQuery] UploadRequest request, [FromForm] IFormFile file)
        {
            if (!await _authManager.HasSitePermissionsAsync(request.SiteId,
                                                            MenuUtils.SitePermissions.WxReplyAuto, MenuUtils.SitePermissions.WxReplyBeAdded))
            {
                return(Unauthorized());
            }

            var site = await _siteRepository.GetAsync(request.SiteId);

            if (file == null)
            {
                return(this.Error("请选择有效的文件上传"));
            }

            MaterialImage image = null;
            MaterialAudio audio = null;
            MaterialVideo video = null;

            if (request.MaterialType == MaterialType.Image)
            {
                var fileName = Path.GetFileName(file.FileName);
                var extName  = PathUtils.GetExtension(fileName);
                if (!_pathManager.IsImageExtensionAllowed(site, extName))
                {
                    return(this.Error("此图片格式已被禁止上传,请转换格式后上传!"));
                }

                var materialFileName     = PathUtils.GetMaterialFileName(fileName);
                var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Image);

                var directoryPath = PathUtils.Combine(_pathManager.WebRootPath, virtualDirectoryPath);
                var filePath      = PathUtils.Combine(directoryPath, materialFileName);

                await _pathManager.UploadAsync(file, filePath);

                await _pathManager.AddWaterMarkAsync(site, filePath);

                image = new MaterialImage
                {
                    GroupId = -request.SiteId,
                    Title   = fileName,
                    Url     = PageUtils.Combine(virtualDirectoryPath, materialFileName)
                };

                await _materialImageRepository.InsertAsync(image);
            }
            else if (request.MaterialType == MaterialType.Audio)
            {
                var fileName = Path.GetFileName(file.FileName);
                var fileType = PathUtils.GetExtension(fileName);
                if (!_pathManager.IsUploadExtensionAllowed(UploadType.Audio, site, fileType))
                {
                    return(this.Error("文件只能是音频格式,请选择有效的文件上传!"));
                }

                var materialFileName     = PathUtils.GetMaterialFileName(fileName);
                var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Audio);

                var directoryPath = PathUtils.Combine(_pathManager.WebRootPath, virtualDirectoryPath);
                var filePath      = PathUtils.Combine(directoryPath, materialFileName);

                await _pathManager.UploadAsync(file, filePath);

                audio = new MaterialAudio
                {
                    GroupId  = -request.SiteId,
                    Title    = PathUtils.RemoveExtension(fileName),
                    FileType = fileType.ToUpper().Replace(".", string.Empty),
                    Url      = PageUtils.Combine(virtualDirectoryPath, materialFileName)
                };

                await _materialAudioRepository.InsertAsync(audio);
            }
            else if (request.MaterialType == MaterialType.Video)
            {
                var fileName = Path.GetFileName(file.FileName);

                var fileType = PathUtils.GetExtension(fileName);
                if (!_pathManager.IsUploadExtensionAllowed(UploadType.Video, site, fileType))
                {
                    return(this.Error("文件只能是视频格式,请选择有效的文件上传!"));
                }

                var materialVideoName    = PathUtils.GetMaterialFileName(fileName);
                var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Video);

                var directoryPath = PathUtils.Combine(_pathManager.WebRootPath, virtualDirectoryPath);
                var filePath      = PathUtils.Combine(directoryPath, materialVideoName);

                await _pathManager.UploadAsync(file, filePath);

                video = new MaterialVideo
                {
                    GroupId  = -request.SiteId,
                    Title    = PathUtils.RemoveExtension(fileName),
                    FileType = fileType.ToUpper().Replace(".", string.Empty),
                    Url      = PageUtils.Combine(virtualDirectoryPath, materialVideoName)
                };

                await _materialVideoRepository.InsertAsync(video);
            }

            return(new UploadResult
            {
                MaterialType = request.MaterialType,
                Image = image,
                Audio = audio,
                Video = video
            });
        }
        public async Task <ActionResult <List <SubmitResult> > > Submit([FromBody] SubmitRequest request)
        {
            var site = await _siteRepository.GetAsync(request.SiteId);

            if (site == null)
            {
                return(this.Error("无法确定内容对应的站点"));
            }

            var result = new List <SubmitResult>();

            foreach (var filePath in request.FilePaths)
            {
                if (string.IsNullOrEmpty(filePath))
                {
                    continue;
                }

                var fileName = PathUtils.GetFileName(filePath);

                var fileExtName        = StringUtils.ToLower(PathUtils.GetExtension(filePath));
                var localDirectoryPath = await _pathManager.GetUploadDirectoryPathAsync(site, fileExtName);

                var virtualUrl = await _pathManager.GetVirtualUrlByPhysicalPathAsync(site, filePath);

                var imageUrl = await _pathManager.ParseSiteUrlAsync(site, virtualUrl, true);

                if (request.IsLibrary)
                {
                    var materialFileName     = PathUtils.GetMaterialFileName(fileName);
                    var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Image);

                    var directoryPath    = _pathManager.ParsePath(virtualDirectoryPath);
                    var materialFilePath = PathUtils.Combine(directoryPath, materialFileName);
                    DirectoryUtils.CreateDirectoryIfNotExists(materialFilePath);

                    FileUtils.CopyFile(filePath, materialFilePath, true);

                    var image = new MaterialImage
                    {
                        GroupId = -request.SiteId,
                        Title   = fileName,
                        Url     = PageUtils.Combine(virtualDirectoryPath, materialFileName)
                    };

                    await _materialImageRepository.InsertAsync(image);
                }

                if (request.IsThumb)
                {
                    var localSmallFileName = Constants.SmallImageAppendix + fileName;
                    var localSmallFilePath = PathUtils.Combine(localDirectoryPath, localSmallFileName);

                    var thumbnailVirtualUrl = await _pathManager.GetVirtualUrlByPhysicalPathAsync(site, localSmallFilePath);

                    var thumbnailUrl = await _pathManager.ParseSiteUrlAsync(site, thumbnailVirtualUrl, true);

                    _pathManager.ResizeImageByMax(filePath, localSmallFilePath, request.ThumbWidth, request.ThumbHeight);

                    if (request.IsLinkToOriginal)
                    {
                        result.Add(new SubmitResult
                        {
                            ImageUrl          = thumbnailUrl,
                            ImageVirtualUrl   = thumbnailVirtualUrl,
                            PreviewUrl        = imageUrl,
                            PreviewVirtualUrl = virtualUrl
                        });
                    }
                    else
                    {
                        FileUtils.DeleteFileIfExists(filePath);
                        result.Add(new SubmitResult
                        {
                            ImageUrl        = thumbnailUrl,
                            ImageVirtualUrl = thumbnailVirtualUrl
                        });
                    }
                }
                else
                {
                    result.Add(new SubmitResult
                    {
                        ImageUrl        = imageUrl,
                        ImageVirtualUrl = virtualUrl
                    });
                }
            }

            var options = TranslateUtils.JsonDeserialize(site.Get <string>(nameof(LayerImageUploadController)), new Options
            {
                IsEditor         = true,
                IsLibrary        = true,
                IsThumb          = false,
                ThumbWidth       = 1024,
                ThumbHeight      = 1024,
                IsLinkToOriginal = true,
            });

            options.IsEditor         = request.IsEditor;
            options.IsLibrary        = request.IsLibrary;
            options.IsThumb          = request.IsThumb;
            options.ThumbWidth       = request.ThumbWidth;
            options.ThumbHeight      = request.ThumbHeight;
            options.IsLinkToOriginal = request.IsLinkToOriginal;
            site.Set(nameof(LayerImageUploadController), TranslateUtils.JsonSerialize(options));

            await _siteRepository.UpdateAsync(site);

            return(result);
        }
Exemplo n.º 6
0
        public async Task PullMaterialAsync(string accessTokenOrAppId, MaterialType materialType, int groupId)
        {
            var count = await MediaApi.GetMediaCountAsync(accessTokenOrAppId);

            if (materialType == MaterialType.Message)
            {
                if (count.news_count > 0)
                {
                    var newsList = await MediaApi.GetNewsMediaListAsync(accessTokenOrAppId, 0, count.news_count);

                    newsList.item.Reverse();

                    foreach (var message in newsList.item)
                    {
                        if (await _materialMessageRepository.IsExistsAsync(message.media_id))
                        {
                            continue;
                        }

                        //var news = await MediaApi.GetForeverNewsAsync(accessTokenOrAppId, message.media_id);
                        var messageItems = new List <MaterialMessageItem>();
                        foreach (var item in message.content.news_item)
                        {
                            var imageUrl = string.Empty;
                            if (!string.IsNullOrEmpty(item.thumb_media_id) && !string.IsNullOrEmpty(item.thumb_url))
                            {
                                await using var ms = new MemoryStream();
                                await MediaApi.GetForeverMediaAsync(accessTokenOrAppId, item.thumb_media_id, ms);

                                ms.Seek(0, SeekOrigin.Begin);

                                var extName = "png";
                                if (StringUtils.Contains(item.thumb_url, "wx_fmt="))
                                {
                                    extName = item.thumb_url.Substring(item.thumb_url.LastIndexOf("=", StringComparison.Ordinal) + 1);
                                }

                                var materialFileName     = PathUtils.GetMaterialFileNameByExtName(extName);
                                var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Image);

                                var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath);
                                var filePath      = PathUtils.Combine(directoryPath, materialFileName);

                                await FileUtils.WriteStreamAsync(filePath, ms);

                                imageUrl = PageUtils.Combine(virtualDirectoryPath, materialFileName);
                            }
                            else if (!string.IsNullOrEmpty(item.thumb_url))
                            {
                                var extName = "png";
                                if (StringUtils.Contains(item.thumb_url, "wx_fmt="))
                                {
                                    extName = item.thumb_url.Substring(item.thumb_url.LastIndexOf("=", StringComparison.Ordinal) + 1);
                                }

                                var materialFileName     = PathUtils.GetMaterialFileNameByExtName(extName);
                                var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Image);

                                var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath);
                                var filePath      = PathUtils.Combine(directoryPath, materialFileName);

                                WebClientUtils.Download(item.thumb_url, filePath);

                                imageUrl = PageUtils.Combine(virtualDirectoryPath, materialFileName);
                            }

                            var commentType = CommentType.Block;
                            if (item.need_open_comment == 1)
                            {
                                commentType = item.only_fans_can_comment == 1 ? CommentType.OnlyFans : CommentType.Everyone;
                            }

                            messageItems.Add(new MaterialMessageItem
                            {
                                MessageId        = 0,
                                MaterialType     = MaterialType.Article,
                                MaterialId       = 0,
                                Taxis            = 0,
                                ThumbMediaId     = item.thumb_media_id,
                                Author           = item.author,
                                Title            = item.title,
                                ContentSourceUrl = item.content_source_url,
                                Content          = SaveImages(item.content),
                                Digest           = item.digest,
                                ShowCoverPic     = item.show_cover_pic == "1",
                                ThumbUrl         = imageUrl,
                                Url         = item.url,
                                CommentType = commentType
                            });
                        }

                        await _materialMessageRepository.InsertAsync(groupId, message.media_id, messageItems);
                    }
                }
            }
            else if (materialType == MaterialType.Image)
            {
                if (count.image_count > 0)
                {
                    var list = await MediaApi.GetOthersMediaListAsync(accessTokenOrAppId, UploadMediaFileType.image, 0, count.image_count);

                    foreach (var image in list.item)
                    {
                        if (await _materialImageRepository.IsExistsAsync(image.media_id))
                        {
                            continue;
                        }

                        await using var ms = new MemoryStream();
                        await MediaApi.GetForeverMediaAsync(accessTokenOrAppId, image.media_id, ms);

                        ms.Seek(0, SeekOrigin.Begin);

                        var extName = image.url.Substring(image.url.LastIndexOf("=", StringComparison.Ordinal) + 1);

                        var materialFileName     = PathUtils.GetMaterialFileNameByExtName(extName);
                        var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Image);

                        var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath);
                        var filePath      = PathUtils.Combine(directoryPath, materialFileName);

                        await FileUtils.WriteStreamAsync(filePath, ms);

                        var material = new MaterialImage
                        {
                            GroupId = groupId,
                            Title   = image.name,
                            Url     = PageUtils.Combine(virtualDirectoryPath, materialFileName),
                            MediaId = image.media_id
                        };

                        await _materialImageRepository.InsertAsync(material);
                    }
                }
            }
            else if (materialType == MaterialType.Audio)
            {
                if (count.voice_count > 0)
                {
                    var list = await MediaApi.GetOthersMediaListAsync(accessTokenOrAppId, UploadMediaFileType.voice, 0, count.voice_count);

                    foreach (var voice in list.item)
                    {
                        if (await _materialAudioRepository.IsExistsAsync(voice.media_id))
                        {
                            continue;
                        }

                        await using var ms = new MemoryStream();
                        await MediaApi.GetForeverMediaAsync(accessTokenOrAppId, voice.media_id, ms);

                        ms.Seek(0, SeekOrigin.Begin);

                        var extName = voice.url.Substring(voice.url.LastIndexOf("=", StringComparison.Ordinal) + 1);

                        var materialFileName     = PathUtils.GetMaterialFileNameByExtName(extName);
                        var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Audio);

                        var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath);
                        var filePath      = PathUtils.Combine(directoryPath, materialFileName);

                        await FileUtils.WriteStreamAsync(filePath, ms);

                        var audio = new MaterialAudio
                        {
                            GroupId  = groupId,
                            Title    = voice.name,
                            FileType = extName.ToUpper().Replace(".", string.Empty),
                            Url      = PageUtils.Combine(virtualDirectoryPath, materialFileName),
                            MediaId  = voice.media_id
                        };

                        await _materialAudioRepository.InsertAsync(audio);
                    }
                }
            }
            else if (materialType == MaterialType.Video)
            {
                if (count.video_count > 0)
                {
                    var list = await MediaApi.GetOthersMediaListAsync(accessTokenOrAppId, UploadMediaFileType.video, 0, count.video_count);

                    foreach (var video in list.item)
                    {
                        if (await _materialVideoRepository.IsExistsAsync(video.media_id))
                        {
                            continue;
                        }

                        await using var ms = new MemoryStream();
                        await MediaApi.GetForeverMediaAsync(accessTokenOrAppId, video.media_id, ms);

                        ms.Seek(0, SeekOrigin.Begin);

                        var extName = "mp4";

                        if (!string.IsNullOrEmpty(video.url))
                        {
                            extName = video.url.Substring(video.url.LastIndexOf("=", StringComparison.Ordinal) + 1);
                        }

                        var materialFileName     = PathUtils.GetMaterialFileNameByExtName(extName);
                        var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Video);

                        var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath);
                        var filePath      = PathUtils.Combine(directoryPath, materialFileName);

                        await FileUtils.WriteStreamAsync(filePath, ms);

                        var material = new MaterialVideo
                        {
                            GroupId  = groupId,
                            Title    = video.name,
                            FileType = extName.ToUpper().Replace(".", string.Empty),
                            Url      = PageUtils.Combine(virtualDirectoryPath, materialFileName),
                            MediaId  = video.media_id
                        };

                        await _materialVideoRepository.InsertAsync(material);
                    }
                }
            }
        }
Exemplo n.º 7
0
 public async Task <bool> UpdateAsync(MaterialImage image)
 {
     return(await _repository.UpdateAsync(image, Q
                                          .CachingRemove(CacheKey)
                                          ));
 }
Exemplo n.º 8
0
 public async Task <int> InsertAsync(MaterialImage image)
 {
     return(await _repository.InsertAsync(image, Q
                                          .CachingRemove(CacheKey)
                                          ));
 }