Ejemplo n.º 1
0
        public async Task <IActionResult> CheckPublishedFile(Guid id, String type)
        {
            var ifModifiedSince = "";

            if (HttpContext.Request.Headers.ContainsKey("If-Modified-Since"))
            {
                ifModifiedSince = HttpContext.Request.Headers["If-Modified-Since"];
            }

            var productArtifact = await ProductService.GetPublishedFile(id, type);

            if (productArtifact == null)
            {
                return(NotFound());
            }

            var updatedArtifact = WebRequestWrapper.GetFileInfo(productArtifact);

            HttpContext.Response.Headers.Add("Last-Modified", updatedArtifact.LastModified?.ToUniversalTime().ToString("r"));
            HttpContext.Response.Headers.Add("Content-Length", updatedArtifact.FileSize.ToString());

            var lastModified = updatedArtifact.LastModified?.ToUniversalTime().ToString("r");

            if (ifModifiedSince.CompareTo(lastModified) == 0)
            {
                return(StatusCode(304));
            }

            return(Ok());
        }
        protected async Task <DateTime?> AddProductArtifactAsync(string key, string value, Guid productId, ProductBuild productBuild)
        {
            var productArtifact = new ProductArtifact
            {
                ProductId      = productId,
                ProductBuildId = productBuild.Id,
                ArtifactType   = key,
                Url            = value
            };
            var updatedArtifact = WebRequestWrapper.GetFileInfo(productArtifact);
            await ProductArtifactRepository.CreateAsync(updatedArtifact);

#pragma warning disable RECS0061 // Warns when a culture-aware 'EndsWith' call is used by default.
            if (key == "version" && updatedArtifact.ContentType == "application/json")
#pragma warning restore RECS0061 // Warns when a culture-aware 'EndsWith' call is used by default.
            {
                var contents = WebClient.DownloadString(value);
                var version  = JsonConvert.DeserializeObject <Dictionary <string, string> >(contents);
                if (version.ContainsKey("version"))
                {
                    productBuild.Version = version["version"];
                    await ProductBuildRepository.UpdateAsync(productBuild);
                }
            }

            return(updatedArtifact.LastModified);
        }
        protected async Task <DateTime?> AddProductArtifactAsync(string key, string value, Product product, ProductBuild productBuild, bool successful)
        {
            var productArtifact = new ProductArtifact
            {
                ProductId      = product.Id,
                ProductBuildId = productBuild.Id,
                ArtifactType   = key,
                Url            = value
            };
            var updatedArtifact  = WebRequestWrapper.GetFileInfo(productArtifact);
            var existingArtifact = await ProductArtifactRepository
                                   .Get().Where(a => a.ProductId == product.Id && a.ProductBuildId == productBuild.Id && a.ArtifactType == key && a.Url == value)
                                   .FirstOrDefaultAsync();

            if (existingArtifact != null)
            {
                // Not sure why we are getting multiple of these, but we don't want multiple entries.
                // Should we ignore it or update?  Ignore for now. Updating threw exceptions
                Log.Information($"Updating Artifact: Id={existingArtifact.Id}");
                updatedArtifact.Id = existingArtifact.Id;
                // await ProductArtifactRepository.UpdateAsync(updatedArtifact);
            }
            else
            {
                var newArtifact = await ProductArtifactRepository.CreateAsync(updatedArtifact);

                Log.Information($"Created Artifact: Id={newArtifact.Id}");
            }

            // On version.json, update the ProductBuild.Version
            if (key == "version" && updatedArtifact.ContentType == "application/json")
            {
                try
                {
                    var contents = WebClient.DownloadString(value);
                    var version  = JsonConvert.DeserializeObject <Dictionary <string, object> >(contents);
                    if (version.ContainsKey("version"))
                    {
                        productBuild.Version = version["version"] as String;
                        await ProductBuildRepository.UpdateAsync(productBuild);

                        if (successful)
                        {
                            product.VersionBuilt = version["version"] as String;
                            await ProductRepository.UpdateAsync(product);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, $"Parsing {key}: {value}");
                }
            }

            // On play-listing-manifest.json, update the Project.DefaultLanguage
            if (key == "play-listing-manifest" && updatedArtifact.ContentType == "application/json")
            {
                try
                {
                    var contents = WebClient.DownloadString(value);
                    var manifest = JsonConvert.DeserializeObject <Dictionary <string, object> >(contents);
                    if (manifest.ContainsKey("default-language"))
                    {
                        var           languageName  = manifest["default-language"] as String;
                        StoreLanguage storeLanguage = await LanguageRepository.Get().Where(lang => lang.Name == languageName).FirstOrDefaultAsync();

                        if (storeLanguage != null)
                        {
                            product.StoreLanguageId = storeLanguage.Id;
                            await ProductRepository.UpdateAsync(product);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, $"Parsing {key}: {value}");
                }
            }

            return(updatedArtifact.LastModified);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> GetPublishedAppDetails(string package)
        {
            // Get the play-listing/manifest.json artifact
            var manifestArtifact = await ProductService.GetPublishedAppDetails(package);

            if (manifestArtifact == null)
            {
                return(NotFound());
            }

            // Get the size of the apk
            var apkArtifact = await ProductService.GetPublishedFile(manifestArtifact.ProductId, "apk");

            if (apkArtifact == null)
            {
                return(NotFound());
            }
            var updatedApkArtifact = WebRequestWrapper.GetFileInfo(apkArtifact);

            // Get the contents of the manifest.json
            var manifestJson = await WebClient.DownloadStringTaskAsync(manifestArtifact.Url);

            var manifest     = JsonConvert.DeserializeObject <ManifestResponse>(manifestJson);
            var url          = manifest.url;
            var titles       = new Dictionary <string, string>(manifest.languages.Count);
            var descriptions = new Dictionary <string, string>(manifest.languages.Count);

            foreach (string language in manifest.languages)
            {
                var title       = "";
                var titleSearch = $"{language}/title.txt";
                var titlePath   = manifest.files.Where(s => s.Contains(titleSearch)).FirstOrDefault();
                if (!string.IsNullOrEmpty(titlePath))
                {
                    title = await WebClient.DownloadStringTaskAsync(url + titlePath);
                }
                titles.Add(language, title.Trim());

                var description       = "";
                var descriptionSearch = $"{language}/short_description.txt";
                var descriptionPath   = manifest.files.Where(s => s.Contains(descriptionSearch)).FirstOrDefault();
                if (!string.IsNullOrEmpty(descriptionPath))
                {
                    description = await WebClient.DownloadStringTaskAsync(url + descriptionPath);
                }
                descriptions.Add(language, description);
            }

            var details = new AppDetails
            {
                Id                 = manifestArtifact.ProductId,
                Link               = $"/api/products/{manifestArtifact.ProductId}/files/published/apk",
                Size               = updatedApkArtifact.FileSize.Value,
                DefaultLanguage    = manifest.defaultLanguage,
                Color              = manifest.color,
                Icon               = url + manifest.icon,
                Languages          = manifest.languages,
                Titles             = titles,
                Descriptions       = descriptions,
                DownloadApkStrings = manifest.downloadApkStrings
            };

            return(Ok(details));
        }