Beispiel #1
0
        public async Task <IActionResult> PutAsync([FromBody] IFormFile file, [FromHeader(Name = "X-PackageVersion")] string version, [FromHeader(Name = "X-ServerDigest")] string digest)
        {
            if (digest != config.ServerDigest)
            {
                return(Unauthorized());
            }

            if (string.IsNullOrEmpty(version))
            {
                return(BadRequest());
            }

            byte[] buffer = new byte[file.Length];

            Stream fileStream = file.OpenReadStream();

            await fileStream.ReadAsync(buffer, 0, buffer.Length);

            using (var client = new HttpClient())
            {
                HttpResponseMessage response =
                    await client.PostAsync(config.FileServiceUrl,
                                           new MultipartFormDataContent
                {
                    { new ByteArrayContent(buffer), "data", file.FileName },
                    { new StringContent(GetVersionFileKey(version)), "key" }
                });

                if (response.IsSuccessStatusCode)
                {
                    string hash = await response.Content.ReadAsStringAsync();

                    VersionRow appVersion = await storeService.AddVersionAsync("spm", version, hash);

                    return(Ok(appVersion));
                }
                else
                {
                    return(BadRequest(await response.Content.ReadAsStringAsync()));
                }
            }
        }