Example #1
10
        public string Execute(FileItem item, string infile, string dest, ValuePairEnumerator configData)
        {
            var conf = new ChromakeyViewModel(configData);
            dest = Path.Combine(Path.GetDirectoryName(dest), Path.GetFileNameWithoutExtension(dest) + ".jpg");

            KalikoImage image = new KalikoImage(infile);
            var x = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(conf.BackgroundColor);
            var filter = new ChromaKeyFilter();
            filter.KeyColor = Color.FromArgb(x.R, x.G, x.B);
            filter.ToleranceHue = conf.Hue;
            filter.ToleranceSaturnation = conf.Saturnation/100f;
            filter.ToleranceBrightness = conf.Brigthness / 100f;
            image.ApplyFilter(filter);
            var res = image.Clone();
            
            if (conf.UnsharpMask)
                res.ApplyFilter(new UnsharpMaskFilter(1.4f, 1.32f, 5));

            var backdrop = new KalikoImage(conf.BackgroundFile);
            backdrop = backdrop.Scale(new FitScaling(image.Width, image.Height));
            backdrop.BlitImage(res);

            backdrop.SaveJpg(dest, 90);
            return dest;
        }
        private string SaveImage() {
            if (!DoesImageNeedHandling) {
                return _originalPath;
            }

            var image = new KalikoImage(Server.MapPath(_originalPath));

            // TODO: Temporary fix for selecting full image, try to do this without loading the image first..
            if (IsCropValueFullImage(image)) {
                return _originalPath;
            }

            if (_hasCropValues) {
                image.Crop(_cropX, _cropY, _cropW, _cropH);
            }

            if (_width > 0 || _height > 0) {
                image.Resize(_width, _height);
            }

            var imagePath = GetNewImagePath();
            var serverPath = Server.MapPath(imagePath);

            if (File.Exists(serverPath)) {
                var originalServerPath = Server.MapPath(_originalPath);

                if (File.GetLastWriteTime(originalServerPath) < File.GetLastWriteTime(serverPath)) {
                    return imagePath;
                }

                File.Delete(serverPath);
            }

            // TODO: Config quality
            image.SaveJpg(Server.MapPath(imagePath), 90);

            return imagePath;
        }
Example #3
0
    IEnumerator UploadCoroutine()
    {
        MessageBoxManager.Instance.ShowMessage(LimLanguageManager.TextDict["Layesta_Submission_Upload1"].Replace("<br>", "\n"));
        yield return(null);

        string path = WindowsDialogUtility.OpenFileDialog(LimLanguageManager.TextDict["Layesta_Submission_Load"],
#if UNITY_EDITOR
                                                          "layesta"
#else
                                                          "Layesta File|*.layesta"
#endif
                                                          , null);

        if (path == null)
        {
            yield break;
        }
        bool validate = LimLayestaReader.Validate(path);

        if (!validate)
        {
            UpdateUploadStatus("Layesta_Submission_UploadErr1");
            yield break;
        }
        byte[]       img   = LimLayestaReader.LoadBackgroundImage(path);
        MemoryStream ms    = new MemoryStream(img);
        MemoryStream msOut = new MemoryStream();

        Kaliko.ImageLibrary.KalikoImage k = new Kaliko.ImageLibrary.KalikoImage(ms);
        k = k.Scale(new Kaliko.ImageLibrary.Scaling.PadScaling(640, 360));
        k.SaveJpg(msOut, 80);
        ms.Close();
        byte[] cov = msOut.GetBuffer();
        msOut.Close();

        UpdateUploadStatus("Layesta_Submission_Upload6");
        #region Update Info
        byte[] buf = lzip.entry2Buffer(path, "info.bytes");
        ms = new MemoryStream(buf);
        BinaryReader br = new BinaryReader(ms);
        level.ShouldDisplay = ShouldDisplay.isOn;
        level.Title         = br.ReadString();
        level.Difficulties  = "";
        br.ReadString();
        int count = br.ReadInt32();
        for (int i = 0; i < count; ++i)
        {
            level.Difficulties += (br.ReadString() + ((i == count - 1) ? "" : " "));
        }
        br.ReadInt32();
        br.ReadInt32();
        if (br.BaseStream.Length > br.BaseStream.Position)
        {
            level.SongArtist = br.ReadString();
        }
        if (br.BaseStream.Length > br.BaseStream.Position)
        {
            br.ReadInt32();
        }
        br.Close();
        ms.Close();

        UnityWebRequest web = new UnityWebRequest
        {
            downloadHandler = new DownloadHandlerBuffer(),
            uploadHandler   = new UploadHandlerRaw(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(level))),
            url             = $"https://la.schwarzer.wang/layestalevel/update",
            method          = "POST"
        };
        web.SetRequestHeader("Authorization", $"Bearer {LimLayestaSubmissionManager.Instance.Bearer}");
        web.SetRequestHeader("Content-Type", $"application/json; charset=utf-8");
        web.SetRequestHeader("Accept", $"application/json; charset=utf-8");
        yield return(web.SendWebRequest());

        if (!string.IsNullOrWhiteSpace(web.error))
        {
            UpdateUploadStatus("Layesta_Submission_UploadErr2");
            yield break;
        }
        Debug.Log(web.downloadHandler.text);
        LayestaLevelResponse infoRet = JsonConvert.DeserializeObject <LayestaLevelResponse>(web.downloadHandler.text);
        if (!infoRet.Succeed)
        {
            if (infoRet.ErrorCode == ErrorCode.MissingInfo)
            {
                UpdateUploadStatus("Layesta_Submission_UploadErr3");
            }
            else
            {
                UpdateUploadStatus("Layesta_Submission_UploadErr2");
            }
            yield break;
        }
        #endregion

        UpdateUploadStatus("Layesta_Submission_Upload2");
        #region Get Layesta Upload
        web = new UnityWebRequest
        {
            downloadHandler = new DownloadHandlerBuffer(),
            url             = $"https://la.schwarzer.wang/auth/oss/upload/layesta/{level.Guid}",
            method          = "GET"
        };
        web.SetRequestHeader("Authorization", $"Bearer {LimLayestaSubmissionManager.Instance.Bearer}");
        yield return(web.SendWebRequest());

        if (web.responseCode == 401)
        {
            LimLayestaSubmissionManager.Instance.TokenInvalid();
            yield break;
        }
        if (!string.IsNullOrWhiteSpace(web.error))
        {
            yield break;
        }
        string  ret      = web.downloadHandler.text;
        JObject response = JObject.Parse(ret);
        if (!response["Succeed"].Value <bool>())
        {
            MessageBoxManager.Instance.ShowMessage(((ErrorCode)response["ErrorCode"].Value <int>()).ToString());
            yield break;
        }
        #endregion
        string layestaUrl = response["Uri"].Value <string>();
        string layestaCb  = response["Callback"].Value <string>();

        UpdateUploadStatus("Layesta_Submission_Upload3");
        #region Get Cover Upload
        web = new UnityWebRequest
        {
            downloadHandler = new DownloadHandlerBuffer(),
            url             = $"https://la.schwarzer.wang/auth/oss/upload/cover/{level.Guid}",
            method          = "GET"
        };
        web.SetRequestHeader("Authorization", $"Bearer {LimLayestaSubmissionManager.Instance.Bearer}");
        yield return(web.SendWebRequest());

        if (web.responseCode == 401)
        {
            LimLayestaSubmissionManager.Instance.TokenInvalid();
            yield break;
        }
        if (!string.IsNullOrWhiteSpace(web.error))
        {
            yield break;
        }
        ret      = web.downloadHandler.text;
        response = JObject.Parse(ret);
        if (!response["Succeed"].Value <bool>())
        {
            MessageBoxManager.Instance.ShowMessage(((ErrorCode)response["ErrorCode"].Value <int>()).ToString());
            yield break;
        }
        #endregion
        string coverUrl = response["Uri"].Value <string>();
        string coverCb  = response["Callback"].Value <string>();

        UpdateUploadStatus("Layesta_Submission_Upload4");
        #region Upload Layesta
        web = new UnityWebRequest
        {
            downloadHandler = new DownloadHandlerBuffer(),
            uploadHandler   = new UploadHandlerRaw(File.ReadAllBytes(path)),
            url             = layestaUrl,
            method          = "PUT"
        };
        web.SetRequestHeader("Content-Type", "");
        web.SetRequestHeader("User-Agent", LimLayestaSubmissionManager.Instance.Id);
        web.SetRequestHeader("x-oss-callback", layestaCb);
        ProgressBarManager.Instance.ShowProgress(() => web.isDone, () => web.uploadProgress);
        yield return(web.SendWebRequest());

        if (!string.IsNullOrWhiteSpace(web.error))
        {
            UpdateUploadStatus("Layesta_Submission_UploadErr2");
            Debug.Log(web.downloadHandler.text);
            yield break;
        }
        ret = web.downloadHandler.text;
        Debug.Log(ret);
        response = JObject.Parse(ret);
        if (response["Message"].Value <string>() != "Succeed")
        {
            UpdateUploadStatus("Layesta_Submission_UploadErr2");
            MessageBoxManager.Instance.Message.text += response["Message"].Value <string>();
            yield break;
        }
        #endregion

        UpdateUploadStatus("Layesta_Submission_Upload5");
        #region Upload Cover
        web = new UnityWebRequest
        {
            downloadHandler = new DownloadHandlerBuffer(),
            uploadHandler   = new UploadHandlerRaw(cov),
            url             = coverUrl,
            method          = "PUT"
        };
        web.SetRequestHeader("Content-Type", "");
        web.SetRequestHeader("User-Agent", LimLayestaSubmissionManager.Instance.Id);
        web.SetRequestHeader("x-oss-callback", coverCb);
        ProgressBarManager.Instance.ShowProgress(() => web.isDone, () => web.uploadProgress);
        yield return(web.SendWebRequest());

        if (!string.IsNullOrWhiteSpace(web.error))
        {
            UpdateUploadStatus("Layesta_Submission_UploadErr2");
            Debug.Log(web.downloadHandler.text);
            yield break;
        }
        ret = web.downloadHandler.text;
        Debug.Log(ret);
        response = JObject.Parse(ret);
        if (response["Message"].Value <string>() != "Succeed")
        {
            UpdateUploadStatus("Layesta_Submission_UploadErr2");
            MessageBoxManager.Instance.Message.text += response["Message"].Value <string>();
            yield break;
        }
        #endregion

        UpdateUploadStatus("Layesta_Submission_Upload7");
        LimLayestaSubmissionManager.Instance.EmptyList();
        LimLayestaSubmissionManager.Instance.Refresh();
    }