Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file-service on centos7.4 Image cropping failure. #9

Open
BootsShu opened this issue Sep 21, 2019 · 0 comments
Open

file-service on centos7.4 Image cropping failure. #9

BootsShu opened this issue Sep 21, 2019 · 0 comments

Comments

@BootsShu
Copy link

BootsShu commented Sep 21, 2019

Running environment :Centos7.4/ASP.NET CORE 2.2

DockerCommand : docker run --name file_service_api -d -v files_data:/publish/wwwroot/ --restart=always -p 8001:80 file_service_api:$image_version

private void ConvertImage(string srcFilePath, Mime srcMime, string dstFilePath,
ImageModifier dstImgMod)
{
var isSvg = false;
if (srcMime.ContentType == "image/svg+xml")
{
var oldSrcFilePath = srcFilePath;
srcFilePath = Path.Combine(srcFilePath, Guid.NewGuid().ToString("N") + ".svg");
File.Copy(oldSrcFilePath, srcFilePath, true);
isSvg = true;
Log.Info(msg: "进入image/svg+xml", isPushMQ: true);
}

        try
        {
            //命令行参考:http://elf8848.iteye.com/blog/382528
            var opt = _option.CurrentValue;
            var pArgs = string.Empty;
            if (dstImgMod.Size != ImageSize.Raw)
            {
                var size = dstImgMod.Size;
                if (size.Width < 1 && size.Height < 1)
                    throw new InvalidOperationException("宽与高不能同时小于1");

                var widthStr = size.Width > 0 ? size.Width.ToString() : "";
                var heightStr = size.Height > 0 ? size.Height.ToString() : "";
                var resizeArgs = RexVar.Replace(_option.CurrentValue.ResizeArgs, m =>
                {
                    var varName = m.Groups[1].Value;
                    switch (varName)
                    {
                        case "width":
                            return widthStr;
                        case "height":
                            return heightStr;
                        default:
                            return m.Value;
                    }
                });

                pArgs += RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? string.Format("-resize '{0}'", resizeArgs) : "-resize \"" + resizeArgs + "\"";
            }

            pArgs += $" \"{srcFilePath}\" \"{dstFilePath}\"";
            var pArgs2 = Path.Combine(srcFilePath, dstFilePath);
            var timeoutSec = opt.ConvertTimeout * 1000;

            try
            {
                //pArgs=-resize '64x64>' "/publish/wwwroot/files/2019/09/21/3e49/25_31/raw" "/publish/wwwroot/files/2019/09/21/3e49/25_31/image_64x64_jpg.cvting.jpg"
                var exitCode = ProcessUtil.ExecuteCommand(opt.ConverterPath, pArgs, out var stdOut, out var errOut, timeoutSec);
                if (exitCode != 0)
                {
                    Log.Info(msg: "convert fail, exitCode=" + exitCode, isPushMQ: true);
                    throw new InvalidOperationException("convert fail, exitCode=" + exitCode);
                }
            }
            catch (Exception ex)
            {
                Log.Error(msg: ex.Message, ex: ex, isPushMQ: true);
                throw ex;
            }

        }
        catch (Exception ex)
        {
            try
            {
                File.Delete(dstFilePath);
            }
            catch
            {
            }
            throw ex;
        }
        finally
        {
            if (isSvg)
                try
                {
                    File.Delete(srcFilePath);
                }
                catch
                {

                }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant