Esempio n. 1
0
        private async Task DownloadIconAsync(string url, string sourceName, string packageName)
        {
            try
            {
                IDownloadService downloadService = downloadFactory.GetDefaultDownloadService();
                byte[]           iconBytes       = null;
                iconBytes = await downloadService.DownloadData(url).ConfigureAwait(false);

                if (IsValidIcon(iconBytes))
                {
                    await File.WriteAllBytesAsync(GetIconFileNameWithCacheFolder(sourceName, packageName), iconBytes).ConfigureAwait(false);
                }
                else
                {
                    var defaultIconBytes = File.ReadAllBytes("wwwroot/assets/package.png"); //TODO: assets folder should be in appsettings
                    await File.WriteAllBytesAsync(GetIconFileNameWithCacheFolder(sourceName, packageName), defaultIconBytes).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "DownloadIconAsync - count not download icon");
            }
        }
Esempio n. 2
0
        public async Task <SourceServerResponseDTO> GetPackages(
            string sourceName,
            string url,
            string arch,
            string model,
            VersionDTO versionDto,
            bool isBeta,
            string customUserAgent,
            bool isSearch,
            string keyword    = null,
            bool useGetMethod = false)
        {
            Stopwatch stopwatch = new Stopwatch();

            string            errorMessage;
            ResultFrom        resultFrom;
            double?           cacheOld   = null;
            ParametersDTO     parameters = new ParametersDTO(sourceName, model, versionDto, isBeta, keyword);
            SearchLogEntryDTO logEntry   = new SearchLogEntryDTO(parameters);

            logEntry.RequestType = isSearch ? RequestType.Search : RequestType.Browse;
            logEntry.LogType     = LogType.Parameters;
            logger.LogInformation(Utils.GetSearchLogEntryString(logEntry));
            logEntry.LogType = LogType.Result;
            stopwatch.Start();
            var cacheResult = await cacheService.GetSpkResponseFromCache(sourceName, arch, model, versionDto.Build.ToString(), isBeta);

            SpkResult result;

            if (!cacheResult.HasValidCache)
            {
                string unique            = $"synology_{arch}_{model}"; //TODO: DSM provide model without leading "DS" or "RS", so we should do the same some day.
                var    parametersRequest = PrepareParametersForRequest(arch, unique, versionDto, isBeta, customUserAgent, out var userAgent);

                IDownloadService downloadService = downloadFactory.GetDefaultDownloadService();
                var response = await downloadService.Execute(url, parametersRequest, userAgent, useGetMethod);

                if (response.Success)
                {
                    try
                    {
                        result     = ParseResponse(sourceName, url, arch, model, versionDto, isBeta, response.Content);
                        resultFrom = ResultFrom.Server;
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"Unable to parse response from {url}: {response.Content}");
                        if (cacheResult.Cache != null)
                        {
                            logger.LogInformation("Returning data from cache");
                            result     = cacheResult.Cache.SpkResult;
                            resultFrom = ResultFrom.Cache;
                            cacheOld   = cacheResult.Cache.CacheOld;
                        }
                        else
                        {
                            logger.LogError($"Error getting response for url: {url}. No cache available.");
                            return(new SourceServerResponseDTO(false, "No data from source server. No cache available", parameters, null, ResultFrom.NotSpecified, null));
                        }
                    }
                }
                else if (cacheResult.Cache != null)
                {
                    logger.LogError($"Error getting response for url: {url}: {response.ErrorMessage}");
                    result     = cacheResult.Cache.SpkResult;
                    resultFrom = ResultFrom.ExpiredCache;
                    cacheOld   = cacheResult.Cache.CacheOld;
                }
                else //no cache && no server response
                {
                    errorMessage = $"{response.ErrorMessage}";
                    logger.LogError($"Error getting response for url: {url}: {errorMessage}");
                    return(new SourceServerResponseDTO(false, errorMessage, parameters, null, ResultFrom.NotSpecified, null));
                }
            }
            else // get data from Valid cache
            {
                result     = cacheResult.Cache.SpkResult;
                resultFrom = ResultFrom.Cache;
                cacheOld   = cacheResult.Cache.CacheOld;
            }

            if (result != null)
            {
                var finalResult = await GenerateResult(sourceName, keyword, parameters, result, resultFrom, cacheOld);

                stopwatch.Stop();
                logEntry.ResultFrom    = resultFrom;
                logEntry.CacheOld      = cacheOld;
                logEntry.ExecutionTime = stopwatch.ElapsedMilliseconds;
                logger.LogInformation(Utils.GetSearchLogEntryString(logEntry));
                return(finalResult);
            }
            else
            {
                errorMessage = "Spk result is empty";
                stopwatch.Stop();
                logEntry.ResultFrom    = ResultFrom.NotSpecified;
                logEntry.CacheOld      = null;
                logEntry.ExecutionTime = stopwatch.ElapsedMilliseconds;
                logger.LogWarning("Spk result is empty {0}", Utils.GetSearchLogEntryString(logEntry));
                return(new SourceServerResponseDTO(false, errorMessage, parameters, null, resultFrom, cacheOld));
            }
        }