public void AddTag(string resourceID, string tag)
        {
            UserRepository ur       = new UserRepository();
            string         username = User.Identity.Name;
            User           user     = ur.Get(username);

            Repository.ResourceRepository repository = new Repository.ResourceRepository();
            TagRepository tagRepository = new TagRepository();

            DigitalResource resource = repository.Get(resourceID, user);

            ResourceModel.Tag tagToAdd = tagRepository.GetOrAdd(tag);

            resource.AddTag(tagToAdd);
        }
        public async Task <HttpResponseMessage> Get(string id)
        {
            bool isPartial = false;

            UserRepository ur       = new UserRepository();
            string         username = User.Identity.Name;
            User           user     = ur.Get(username);

            Repository.ResourceRepository repository = new Repository.ResourceRepository();
            DigitalResource resource = repository.Get(id, user);
            //await stream.CopyToAsync(stream2,);
            // Create a client
            AmazonS3Client client = new AmazonS3Client();

            long from = 0;
            long to   = 0;

            GetObjectRequest request = null;

            if (Request.Headers.Range != null)
            {
                isPartial = true;
                //If the last-byte-pos value is absent, or if the value is greater than or equal to the current length of the entity-body,
                //last -byte-pos is taken to be equal to one less than the current length of the entity- body in bytes.
                //*-The final 500 bytes(byte offsets 9500 - 9999, inclusive):
                //    bytes = -500
                //  - Or bytes = 9500 -
                if (Request.Headers.Range.Ranges.First().From == null)
                {
                    from = (resource.Size - 1) - (long)Request.Headers.Range.Ranges.First().To;
                }
                else //From byte is specified
                {
                    from = (long)Request.Headers.Range.Ranges.First().From;
                }

                to = resource.Size - 1;
                if (Request.Headers.Range.Ranges.First().To != null)
                {
                    to = (long)Request.Headers.Range.Ranges.First().To;
                }
                else//Chrome often sends out a 0- request. Instead of passing back a massive file, just pass back the first 500 bytes
                {
                    if (from == 0)
                    {
                        to = 499;
                    }
                }
                request = new GetObjectRequest
                {
                    BucketName = "piccoli",
                    Key        = id,
                    ByteRange  = new ByteRange(from, to)
                };
            }
            else
            {
                request = new GetObjectRequest
                {
                    BucketName = "piccoli",
                    Key        = id
                };
            }
            // Issue request and remember to dispose of the response
            //using
            GetObjectResponse   response = client.GetObject(request);
            HttpStatusCode      status   = isPartial ? HttpStatusCode.PartialContent : HttpStatusCode.OK;
            HttpResponseMessage result   = new HttpResponseMessage(status);

            result.Content = new StreamContent(response.ResponseStream);
            string contentType = response.Headers["Content-Type"];

            result.Content.Headers.ContentLength = resource.Size;
            if (isPartial)
            {
                result.Content.Headers.ContentLength = to - from + 1;//resource.Size;
                result.Content.Headers.ContentRange  = new ContentRangeHeaderValue(from, to, resource.Size);
            }
            result.Content.Headers.ContentType =
                new MediaTypeHeaderValue(contentType);//contentType);
            return(result);
        }
        public HttpResponseMessage GetThumbnailRepresentation(string id)
        {
            UserRepository ur       = new UserRepository();
            string         username = User.Identity.Name;
            User           user     = ur.Get(username);

            int thumbnailHeight = 120;

            Repository.ResourceRepository repository = new Repository.ResourceRepository();
            DigitalResource resource = repository.Get(id, user);
            string          mimeType = String.Empty;

            if (resource.OriginalFileName.IndexOf(".") > 0)
            {
                string extension = resource.OriginalFileName.Substring(resource.OriginalFileName.LastIndexOf(".") + 1);
                mimeType = MimeTypeHelper.GetMimeType(resource.OriginalFileName.Substring(resource.OriginalFileName.LastIndexOf(".") + 1));
            }

            //await stream.CopyToAsync(stream2,);
            // Create a client
            AmazonS3Client client = new AmazonS3Client();

            GetObjectRequest request = null;

            request = new GetObjectRequest
            {
                BucketName = "piccoli",
                Key        = id
            };

            // Issue request and remember to dispose of the response
            //using
            GetObjectResponse   response = client.GetObject(request);
            HttpResponseMessage result   = new HttpResponseMessage(HttpStatusCode.OK);


            if (mimeType.Contains("image"))
            {
                Image myImage = Image.FromStream(response.ResponseStream);

                float        multiplicationRatio = (float)thumbnailHeight / (float)myImage.Height;
                int          thumbnailWidth      = (int)(multiplicationRatio * myImage.Width);
                Bitmap       bmp          = ResizeImage(myImage, thumbnailWidth, thumbnailHeight);
                MemoryStream memoryStream = new MemoryStream();
                bmp.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                memoryStream.Position = 0;
                result.Content        = new StreamContent(memoryStream);
                result.Content.Headers.ContentType =
                    new MediaTypeHeaderValue("application/octet-stream");
            }
            //else if (mimeType.Contains("video"))
            //{
            //    VideoFileReader reader = new VideoFileReader();
            //    reader.
            //    Bitmap bmp = reader.ReadVideoFrame();
            //    MemoryStream memoryStream = new MemoryStream();
            //    bmp.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //    memoryStream.Position = 0;
            //    result.Content = new StreamContent(memoryStream);
            //    result.Content.Headers.ContentType =
            //        new MediaTypeHeaderValue("application/octet-stream");
            //}
            else
            {
                MemoryStream memoryStream = TextToImage(200, 120, "Non image resource");
                result.Content = new StreamContent(memoryStream);
                result.Content.Headers.ContentType =
                    new MediaTypeHeaderValue("application/octet-stream");
            }

            return(result);
        }