Ejemplo n.º 1
0
        public async Task <IActionResult> PutMemeItem([FromRoute] int id, [FromBody] MemeItem memeItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != memeItem.Id)
            {
                return(BadRequest());
            }

            _context.Entry(memeItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MemeItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> UploadFile([FromForm] MemeImageItem meme)
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }
            try
            {
                using (var stream = meme.Image.OpenReadStream())
                {
                    var cloudBlock = await UploadToBlob(meme.Image.FileName, null, stream);

                    //// Retrieve the filename of the file you have uploaded
                    //var filename = provider.FileData.FirstOrDefault()?.LocalFileName;
                    if (string.IsNullOrEmpty(cloudBlock.StorageUri.ToString()))
                    {
                        return(BadRequest("An error has occured while uploading your file. Please try again."));
                    }

                    MemeItem memeItem = new MemeItem();
                    memeItem.Title = meme.Title;
                    memeItem.Tags  = meme.Tags;

                    System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
                    memeItem.Height   = image.Height.ToString();
                    memeItem.Width    = image.Width.ToString();
                    memeItem.Url      = cloudBlock.SnapshotQualifiedUri.AbsoluteUri;
                    memeItem.Uploaded = DateTime.Now.ToString();

                    _context.MemeItem.Add(memeItem);
                    await _context.SaveChangesAsync();

                    return(Ok($"File: {meme.Title} has successfully uploaded"));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest($"An error has occured. Details: {ex.Message}"));
            }
        }