예제 #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            int mediaId = int.Parse(Request.QueryString["mediaId"]);

            var db = new RentItDatabaseDataContext();
            if (!db.Medias.Exists(media => media.id == mediaId))
            {
                Response.StatusCode = 400;
                Response.StatusDescription = "The requested media does not exist.";
                Response.End();
            }

            byte[] thumbnail = db.Medias.Where(media => media.id == mediaId).First().thumbnail;

            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", "thumbnail.jpg"));
            Response.AddHeader("Content-Length", thumbnail.Length.ToString());
            Response.BinaryWrite(thumbnail);
            Response.End();
        }
예제 #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            Stream inputStream = Request.InputStream;

            int mediaId = int.Parse(Request.QueryString["mediaId"]);
            // string mediaExtension = Request.QueryString["extension"];
            string userName = Request.QueryString["userName"];
            string password = Request.QueryString["password"];

            // Check if the user name is a publisher account.
            var db = new RentItDatabase.RentItDatabaseDataContext();
            if (!db.Publisher_accounts.Exists(acc => acc.user_name.Equals(userName)))
            {
                Response.StatusCode = 400;
                Response.StatusDescription = "The specified account is not authorized to upload media.";
            }

            // Check if the metadata of the movie has been uploaded.
            if (!db.Medias.Exists(media => media.id == mediaId))
            {
                Response.StatusCode = 400;
                Response.StatusDescription =
                    "Metadata for the specified media has not been uploaded."
                    + "This is needed before it is possible to upload the data.";
                Response.End();
            }

            var client = new RentItClient();
            try
            {
                client.ValidateCredentials(
                    new AccountCredentials() { UserName = userName, HashedPassword = password });
            }
            catch (Exception)
            {
                Response.StatusCode = 400;
                Response.StatusDescription = "Incorrect credentials";
                Response.End();
            }

            var memoryStream = new MemoryStream();
            byte[] thumbnail;

            try
            {
                inputStream.CopyTo(memoryStream);
                thumbnail = memoryStream.ToArray();
            }
            catch (Exception)
            {
                Response.StatusCode = 400;
                Response.StatusDescription = "Thumbnail upload failed. Please try again.";
                Response.End();
                return;
            }

            RentItDatabase.Media mediaInfo = (from m in db.Medias
                                              where m.id == mediaId
                                              select m).First();
            mediaInfo.thumbnail = thumbnail;
            db.SubmitChanges();
        }
예제 #3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            int mediaId = int.Parse(Request.QueryString["mediaId"]);
            string mediaExtension = Request.QueryString["extension"];
            string userName = Request.QueryString["userName"];
            string password = Request.QueryString["password"];

            Debug.WriteLine("Query string accepted.");

            // Check if the user name is a publisher account.
            var db = new RentItDatabase.RentItDatabaseDataContext();
            if (!db.Publisher_accounts.Exists(acc => acc.user_name.Equals(userName)))
            {
                Response.StatusCode = 400;
                Response.StatusDescription = "The specified account is not authorized to upload media.";
                Response.End();
            }

            // Check if the metadata of the movie has been uploaded.
            if (!db.Medias.Exists(media => media.id == mediaId))
            {
                Response.StatusCode = 400;
                Response.StatusDescription =
                    "Metadata for the specified media has not been uploaded."
                    + "This is needed before it is possible to upload the data.";
                Response.End();
            }

            var client = new RentItClient();
            try
            {
                client.ValidateCredentials(
                    new AccountCredentials() { UserName = userName, HashedPassword = password });
            }
            catch (Exception)
            {
                Response.StatusCode = 400;
                Response.StatusDescription = "Incorrect credentials";
                Response.End();
            }

            // Get the uploaded file.
            HttpPostedFile uploadedFile = Request.Files.Get(0);
            var memoryStream = new MemoryStream();
            byte[] binary;

            try
            {
                uploadedFile.InputStream.CopyTo(memoryStream);
                binary = memoryStream.ToArray();
            }
            catch (Exception)
            {
                Response.StatusCode = 400;
                Response.StatusDescription = "Media upload failed. Please try again.";
                Response.End();
                return;
            }

            RentItDatabase.Media mediaInfo = (from m in db.Medias
                                              where m.id == mediaId
                                              select m).Single();

            // Create a new Media_file entry in the database and upload the file to
            // the database.
            RentItDatabase.Media_file mediaFile = new Media_file
                {
                    name = mediaInfo.title,
                    extension = mediaExtension,
                    data = binary
                };
            db.Media_files.InsertOnSubmit(mediaFile);
            db.SubmitChanges();

            // Connect the Media_file tuple to the Media tuple.
            mediaInfo.Media_file = mediaFile;
            db.SubmitChanges();
        }
예제 #4
0
파일: GetMedia.aspx.cs 프로젝트: ltj/rentit
        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = Request.QueryString["userName"];
            string password = Request.QueryString["password"];
            int mediaId = int.Parse(Request.QueryString["mediaId"]);

            RentItClient client = new RentItClient();

            try
            {
                client.ValidateCredentials(
                    new AccountCredentials() { UserName = userName, HashedPassword = password });
            }
            catch (Exception)
            {
                Response.StatusCode = 400;
                Response.StatusDescription = "Incorrect credentials";
                Response.End();
            }

            RentItDatabaseDataContext db = new RentItDatabaseDataContext();

            // Check if the requested media exists
            if (!db.Medias.Exists(media => media.id == mediaId))
            {
                Response.StatusCode = 400;
                Response.StatusDescription = "Requested media does not exist.";
                Response.End();
            }

            // Get the media metadata from the database.
            var mediaMetadata = (from media in db.Medias
                                 where media.id == mediaId
                                 select media).First();

            // Does any active rentals exist for the specified user?
            bool existsRental;

            int songType = (from mediaT in db.Media_types
                            where mediaT.name.Equals("Song")
                            select mediaT).Single().id;

            // If a song is requested, check if there is a rental for it's album.
            if (mediaMetadata.type_id == songType)
            {
                var album = mediaMetadata.Song.Album_songs.First().Album;
                existsRental =
                    db.Rentals.Exists(
                        rental =>
                        rental.user_name.Equals(userName) && rental.media_id == album.media_id
                        && rental.end_time > DateTime.Now);
            }
            else
            {
                existsRental =
                    db.Rentals.Exists(
                        rental =>
                        rental.user_name.Equals(userName) && rental.media_id == mediaId
                        && rental.end_time > DateTime.Now);
            }

            // Check if the requested media is rented by the user.
            if (!existsRental)
            {
                Response.StatusCode = 400;
                Response.StatusDescription = "The specified user has not currently an active rental of the requested media.";
                Response.End();
            }

            // Get the media data from the database.
            var mediaData = mediaMetadata.Media_file;

            // If a media has not been uploaded to the database of the requested media,
            // return af default media file.
            if (mediaData == null)
            {
                string filePath = "";

                switch (mediaMetadata.Media_type.id)
                {
                    case 2: // Book
                        filePath = @"C:\RentItServices\RentIt01\defaultMedia\book.pdf";
                        break;
                    case 3: //Movie
                        filePath = @"C:\RentItServices\RentIt01\defaultMedia\movie.mp4";
                        break;
                    case 4: // Song
                        filePath = @"C:\RentItServices\RentIt01\defaultMedia\song.mp3";
                        break;
                }

                var fileInfo = new System.IO.FileInfo(filePath);
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.WriteFile(filePath);
                Response.End();
            }
            else
            {
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", mediaMetadata.title + mediaData.extension));
                Response.AddHeader("Content-Length", mediaData.data.Length.ToString());
                Response.BinaryWrite(mediaData.data);
                Response.End();
            }

            /*
            String filePath = @"C:\RentItServices\RentIt01\test.bin";

            var fileInfo = new System.IO.FileInfo(filePath);
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));
            Response.AddHeader("Content-Length", fileInfo.Length.ToString());
            Response.WriteFile(filePath);
            //Response.WriteFile(filePath);
            Response.End();
             * */
        }