コード例 #1
0
        public async Task<IHttpActionResult> PutSubmission(int id, Submission submission)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            db.Entry(submission).State = EntityState.Modified;

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

            return StatusCode(HttpStatusCode.NoContent);
        }
コード例 #2
0
        public void GetThumbnail(ref Submission submission, string mimeType)
        {
            if (submission.Type == SubmissionTypes.Image || submission.Type == SubmissionTypes.Video)
            {
                var mime = mimeType.Split('/');

                var fileName = "images/posts/" + submission.AuthorId + "-" + submission.Date.ToBinary() + "." + mime[1];
                var imgName = "images/posts/" + submission.AuthorId + "-" + submission.Date.ToBinary() + ".jpg";

                // Create folders if they don't exist already
                if (!Directory.Exists(GetServerPath("images")))
                {
                    Directory.CreateDirectory(GetServerPath("images"));
                }
                if (!Directory.Exists(GetServerPath("images/posts")))
                {
                    Directory.CreateDirectory(GetServerPath("images/posts"));
                }

                if (submission.IsHosted)
                {
                    using (var webClient = new WebClient())
                    {
                        webClient.DownloadFile(submission.Url, GetServerPath(fileName));
                        submission.Url = fileName;
                    }
                }

                submission.Img = submission.Url;

                try
                {
                    if (submission.Type == SubmissionTypes.Image && mime[1] == "gif")
                    {
                        submission.Img = imgName;

                        var path = submission.IsHosted ? GetServerPath(submission.Url) : submission.Url;
                        using (var img = System.Drawing.Image.FromFile(path))
                        {
                            var imgPath = GetServerPath(submission.Img);
                            img.Save(imgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                    }
                    else if (submission.Type == SubmissionTypes.Video)
                    {
                        submission.Img = imgName;

                        var path = submission.IsHosted ? GetServerPath(submission.Url) : submission.Url;
                        var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
                        ffMpeg.GetVideoThumbnail(path, GetServerPath(submission.Img), 0);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.GetBaseException());
                }
            }
        }
コード例 #3
0
 public void RemoveImages(Submission submission)
 {
     if (File.Exists(GetServerPath(submission.Img)))
     {
         File.Delete(GetServerPath(submission.Img));
     }
     if (File.Exists(GetServerPath(submission.Url)))
     {
         File.Delete(GetServerPath(submission.Url));
     }
 }
コード例 #4
0
 public void RemoveImages(Submission submission)
 {
     if (!submission.Img.StartsWith("http") && File.Exists(GetServerPath(submission.Img)))
     {
         File.Delete(GetServerPath(submission.Img));
     }
     if (!submission.Url.StartsWith("http") && File.Exists(GetServerPath(submission.Url)))
     {
         File.Delete(GetServerPath(submission.Url));
     }
 }
コード例 #5
0
        public async Task<IHttpActionResult> PostSubmission(Submission submission)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            
            string contentType = "";
            var request = HttpWebRequest.Create(submission.Url) as HttpWebRequest;
            if (request != null)
            {
                var response = request.GetResponse() as HttpWebResponse;

                if (response != null)
                    contentType = response.ContentType;
            }

            if (contentType.Contains("video")) {
                submission.Type = SubmissionTypes.Video;
            }

            if (submission.Type == SubmissionTypes.Spotify ||
                submission.Type == SubmissionTypes.YouTube)
            {
                submission.IsHosted = false;
            }

            _controller.GetThumbnail(ref submission, contentType);

            db.Submissions.Add(submission);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = submission.Id }, submission);
        }