Пример #1
0
        private HandyUploadResponse PostScriptToHandyfeeling(string filename, string csv)
        {
            try
            {
                const string uploadUrl   = "https://www.handyfeeling.com/api/sync/upload";
                string       name        = Path.GetFileNameWithoutExtension(filename);
                string       csvFileName = $"{name}_{DateTime.UtcNow:yyyyMMddHHmmssf}.csv";

                var requestContent = new MultipartFormDataContent();

                var fileContent = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(csv)));

                fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name     = "syncFile",
                    FileName = "\"" + csvFileName + "\""
                };

                fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
                requestContent.Add(fileContent, name, csvFileName);

                using (var client = GetClient())
                {
                    var request                  = client.PostAsync(uploadUrl, requestContent);
                    var responseString           = request.Result.Content.ReadAsStringAsync().Result;
                    HandyUploadResponse response = JsonConvert.DeserializeObject <HandyUploadResponse>(responseString);
                    return(response);
                }
            }
            catch (Exception ex)
            {
                return(new HandyUploadResponse
                {
                    converted = false,
                    error = ex.Message,
                    info = "Try local script hosting or connecting via bluetooth instead",
                    success = false
                });
            }
        }
Пример #2
0
        public void SetScript(string scriptTitle, IEnumerable <FunScriptAction> actions)
        {
            string csvData    = GenerateCsvFromActions(actions.ToList());
            long   scriptSize = Encoding.UTF8.GetByteCount(csvData);

            Debug.WriteLine("Script-Size: " + scriptSize);

            // the maximum size for the script is 1MB
            if (scriptSize <= 1024 * (1024 - 5)) // 1MB - 5kb just in case
            {
                string scriptUrl = null;

                switch (_host)
                {
                case HandyHost.Local:
                {
                    LocalScriptServer.LoadedScript = csvData;
                    IsScriptLoaded = true;
                    scriptUrl      = LocalScriptServer.ScriptHostUrl + "tmp.csv";
                    break;
                }

                case HandyHost.HandyfeelingCom:
                {
                    HandyUploadResponse response = PostScriptToHandyfeeling(scriptTitle + ".csv", csvData);

                    if (response.success)
                    {
                        scriptUrl      = response.url;
                        IsScriptLoaded = true;
                    }
                    else
                    {
                        Debug.WriteLine("Failed to upload script");
                        MessageBox.Show($"Failed to upload script to handyfeeling.com.\n{response.error}\n{response.info}");
                        return;
                    }

                    break;
                }
                }

                SetSyncMode();
                SyncPrepare(new HandyPrepare
                {
                    name    = scriptTitle,
                    url     = scriptUrl,
                    size    = (int)scriptSize,
                    timeout = 20000
                });
            }
            else
            {
                Debug.WriteLine("Failed to load script larger than 1MB");
                OnOsdRequest("The script is to large for the Handy.", TimeSpan.FromSeconds(10), "TheHandyScriptError");

                IsScriptLoaded = false;

                if (_host == HandyHost.Local)
                {
                    LocalScriptServer.LoadedScript = null;
                }
            }
        }