Exemplo n.º 1
0
        public async Task <IHttpActionResult> Upload()
        {
            try
            {
                HttpPostedFile uploadedFile = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;
                if (uploadedFile != null && uploadedFile.ContentLength > 0)
                {
                    if (uploadedFile.FileName != DefaultToolkitNames.TelimenaAssemblyName && !uploadedFile.FileName.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(this.BadRequest(
                                   $"Incorrect file. Expected {DefaultToolkitNames.TelimenaAssemblyName} or {DefaultToolkitNames.ZippedPackageName}"));
                    }
                    TelimenaToolkitData pkg =
                        await this.work.ToolkitDataRepository.StorePackageAsync(false, false, uploadedFile.InputStream, this.fileSaver).ConfigureAwait(false);

                    await this.work.CompleteAsync().ConfigureAwait(false);

                    return(this.Ok($"Uploaded package {pkg.Version} with ID {pkg.Id}"));
                }

                return(this.BadRequest("Empty attachment"));
            }
            catch (Exception ex)
            {
                return(this.BadRequest(ex.Message));
            }
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> Get(Guid id)
        {
            TelimenaToolkitData toolkitData = await this.work.ToolkitDataRepository.FirstOrDefaultAsync(x
                                                                                                        => x.TelimenaPackageInfo != null && x.TelimenaPackageInfo.PublicId == id).ConfigureAwait(false);

            if (toolkitData == null)
            {
                return(this.BadRequest($"Toolkit id [{id}] does not exist"));
            }

            byte[] bytes = await this.work.ToolkitDataRepository.GetPackage(toolkitData.Id, this.fileRetriever).ConfigureAwait(false);

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(bytes)
            };

            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = toolkitData.TelimenaPackageInfo.ZippedFileName
            };
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");

            return(this.ResponseMessage(result));
        }