Ejemplo n.º 1
0
        private async Task <bool> DownloadTool(UserIdentity identity, string payloadId, string hashCode, DeviceToolPayloadMode mode)
        {
            bool result = false;

            if (identity == null)
            {
                throw new Exception("Identity not defined!");
            }

            try
            {
                var assemblyPath = GetPluginFilePath(GetPluginFileName(Guid.NewGuid().ToString()));

                var query = HttpUtility.ParseQueryString(string.Empty);

                query["uniqueId"] = payloadId;
                query["hashCode"] = hashCode;
                query["mode"]     = $"{(int)mode}";

                var url = UrlHelper.BuildUrl(CloudAdapter.Url, "api/description/payload-download", query);

                var json = await CloudAdapter.Get(identity, url);

                if (!string.IsNullOrEmpty(json))
                {
                    var payload = json.TryDeserializeObject <DeviceToolPayload>();

                    if (payload != null)
                    {
                        var base64EncodedBytes = Convert.FromBase64String(payload.Content);

                        File.WriteAllBytes(assemblyPath, base64EncodedBytes);

                        PluginStore.Add(payloadId, assemblyPath);

                        PluginStore.Serialize();

                        result = true;
                    }
                }
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - DownloadTool", e);
            }

            return(result);
        }