partial void DeleteMedia_file(Media_file instance);
 partial void InsertMedia_file(Media_file instance);
 partial void UpdateMedia_file(Media_file instance);
Exemple #4
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();
        }