Exemplo n.º 1
0
        private void InstallCollections(ModuleInstallRequest request)
        {
            const string collection       = "collections";
            var          collectionFilter = @"{name: /^content\/" + collection + "/, " + CommonConst.CommonField.MODULE_NAME + ": '" + request.Name + "', " + CommonConst.CommonField.VERSION + ": '" + request.Version + "'}";

            foreach (var item in _dbService.Get(CommonConst.Collection.MODULE_FILE_UPLOAD_CACHE, new RawQuery(collectionFilter)))
            {
                var fileSourceId   = item[CommonConst.CommonField.DISPLAY_ID].ToString();
                var fileName       = item[CommonConst.CommonField.NAME].ToString();
                var fileSize       = int.Parse(item[CommonConst.CommonField.FILE_SIZE].ToString());
                var contentType    = Mime.GetMimeType(fileName);
                var fileData       = JObjectHelper.GetJObjectDbDataFromFile(fileName, contentType, "content/wwwroot", request.Name, fileSize);
                var id             = fileData[CommonConst.CommonField.DISPLAY_ID].ToString();
                var collectionName = new FileInfo(fileName).Name.Replace(CommonConst.CONFIG_FILE_EXTENSION, "");
                var parent         = new FileInfo(fileName).Directory.Name;
                _logger.Debug($"InstallCollection File : {fileName}, Collection {collectionName}, Parent: { parent}");

                foreach (JObject joData in JObjectHelper.GetJArrayFromString(CommonUtility.GetStringFromBase64(_keyValueStorage.Get <string>(CommonConst.Collection.MODULE_FILE_UPLOAD_CACHE, fileSourceId))))
                {
                    try
                    {
                        joData[CommonConst.CommonField.DISPLAY_ID]             = CommonUtility.GetNewID();
                        joData[CommonConst.CommonField.CREATED_DATA_DATE_TIME] = DateTime.Now;
                        joData[CommonConst.CommonField.MODULE_NAME]            = request.Name;
                        joData[CommonConst.CommonField.VERSION]     = request.Version;
                        joData[CommonConst.CommonField.ÌS_OVERRIDE] = false;
                        joData[CommonConst.CommonField.OVERRIDE_BY] = CommonConst.CommonValue.NONE;
                        var url = GetUIAppUrl(parent);
                        if (string.IsNullOrEmpty(url))
                        {
                            WriteToDB(joData, request.Name, collectionName, CommonConst.CommonField.DATA_KEY);
                        }
                        else
                        {
                            _logger.Debug($"Callling remote /ui/installcollection Flile : {fileName}, Collection {collectionName}, Parent: { parent}, url {url}");
                            joData[CommonConst.CommonValue.COLLECTION] = collectionName;
                            _apiGateway.CallAsync(CommonConst.ActionMethods.POST, "/ui/installcollection", "", joData, null, url).GetAwaiter().GetResult();
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error($"Error InstallCollections collection:{joData}", ex);
                    }
                }
            }
        }