示例#1
0
        /// <summary>
        /// Moves all data and attribute files from one location to another. The
        /// empty folders remaining from the source storage can optionally be
        /// removed when the move is completed.
        /// </summary>
        public static int MoveAll(OSBLEDirectory from,
                                  OSBLEDirectory to, bool deleteFromFoldersWhenDone)
        {
            int moved = 0;

            string[] srcFiles = System.IO.Directory.GetFiles(from.m_dataDir);
            foreach (string srcData in srcFiles)
            {
                string srcAttr = AttributableFileCollection.GetAttrFileName(
                    from.m_attrDir, srcData);

                // Move both files
                System.IO.File.Move(srcData, Path.Combine(to.m_dataDir, Path.GetFileName(srcData)));
                System.IO.File.Move(srcAttr, Path.Combine(to.m_attrDir, Path.GetFileName(srcAttr)));

                moved++;
            }

            // Delete the directories from the source, if it was requested
            if (deleteFromFoldersWhenDone)
            {
                System.IO.Directory.Delete(from.m_dataDir, true);
                System.IO.Directory.Delete(from.m_attrDir, true);
            }

            return(moved);
        }
示例#2
0
        public ActionResult SaveABET()
        {
            // Get the file storage for this assignment's submissions
            OSBLE.Models.FileSystem.AssignmentFilePath afs =
                OSBLE.Models.FileSystem.Directories.GetAssignment(
                    Convert.ToInt32(Request.Form["hdnCourseID"]),
                    Convert.ToInt32(Request.Form["hdnAssignmentID"]));

            foreach (string key in Request.Form.AllKeys)
            {
                if (key.StartsWith("slctProficiency"))
                {
                    // The key will end with the team ID
                    int teamID;
                    if (!int.TryParse(key.Substring(key.IndexOf('y') + 1), out teamID))
                    {
                        continue;
                    }

                    // Get the submission for the team
                    OSBLE.Models.FileSystem.OSBLEDirectory attrFP =
                        afs.Submission(teamID) as OSBLE.Models.FileSystem.OSBLEDirectory;
                    OSBLEFile file = attrFP.FirstFile;
                    if (null == file)
                    {
                        continue;
                    }

                    // Update the attribute for the submission and save
                    file.ABETProficiencyLevel = Request.Form[key];
                    file.SaveAttrs();
                }
            }

            // Send the user back to the assignments index page
            return(RedirectToRoute(new { action = "Index", controller = "Assignment", area = "" }));
        }
示例#3
0
        public ActionResult Index(Assignment model)
        {
            Assignment = model;
            if (ModelState.IsValid)
            {
                WasUpdateSuccessful = true;

                Assignment.CourseID = ActiveCourseUser.AbstractCourseID;

                //CourseController cc = new CourseController();
                //int utcOffset = (ActiveCourseUser.AbstractCourse as Course).TimeZoneOffset;
                //TimeZoneInfo tz = cc.getTimeZone(utcOffset);

                //Assignment.ReleaseDate = TimeZoneInfo.ConvertTimeToUtc(Assignment.ReleaseDate, tz);
                //Assignment.DueDate = TimeZoneInfo.ConvertTimeToUtc(Assignment.DueDate, tz);

                // Put times back into UTC
                Assignment.ReleaseDate = Assignment.ReleaseDate.CourseToUTC(Assignment.CourseID);
                Assignment.DueDate     = Assignment.DueDate.CourseToUTC(Assignment.CourseID);

                //update our DB
                if (Assignment.ID == 0)
                {
                    //Add default assignment teams
                    SetUpDefaultAssignmentTeams();
                    db.Assignments.Add(Assignment);
                }
                else //editing preexisting assingment
                {
                    if (Assignment.AssociatedEventID.HasValue)
                    {
                        //If the assignment is being edited, update it's associated event.
                        Event assignmentsEvent = db.Events.Find(Assignment.AssociatedEventID.Value);
                        EventController.UpdateAssignmentEvent(Assignment, assignmentsEvent, ActiveCourseUser.ID, db);
                    }
                    db.Entry(Assignment).State = System.Data.EntityState.Modified;
                }
                try
                {
                    db.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                }
            }
            else
            {
                WasUpdateSuccessful = false;
            }

            // At this point if the assignment ID is not 0 and the temporary ID does
            // not match the assignment ID then we need to check if we need to move
            // files that were uploaded to a temporary location over to the
            // permenant location.
            int tempID = Convert.ToInt32(Request.Form["temporaryAssignmentID"]);

            if (0 != Assignment.ID && Assignment.ID != tempID)
            {
                OSBLE.Models.FileSystem.OSBLEDirectory temp =
                    OSBLE.Models.FileSystem.Directories.GetAssignment(
                        Assignment.CourseID.Value, tempID)
                    .AttributableFiles;
                OSBLE.Models.FileSystem.OSBLEDirectory perm =
                    OSBLE.Models.FileSystem.Directories.GetAssignment(
                        Assignment.CourseID.Value, Assignment.ID)
                    .AttributableFiles;
                OSBLE.Models.FileSystem.OSBLEDirectory.MoveAll(temp, perm, true);

                // After the move, we need to make sure that we update attributes because
                // the attributes will label the files as assignment descriptions/solutions
                // for an assignment with the temporary ID.
                perm.ReplaceSysAttrAll("assignment_description",
                                       tempID.ToString(), Assignment.ID.ToString());
                perm.ReplaceSysAttrAll("assignment_solution",
                                       tempID.ToString(), Assignment.ID.ToString());
            }

            return(base.PostBack(Assignment));
        }
示例#4
0
        public void ProcessRequest(HttpContext context)
        {
            HttpFileCollection coll;

            //yc: set the max upload size

            try
            {
                coll = context.Request.Files;
                context.Response.ContentType = "text/xml";

                if (0 == coll.Count)
                {
                    WriteErrorResponse(context,
                                       "Course file upload service requires one or more files in the request. " +
                                       "It's possible that your browser did not correctly send the file data " +
                                       "and you may need to update your browser if the problem persists.");
                    return;
                }

                // We're expecting the course ID to be in a parameter (required)
                string courseIDParam = context.Request.Params["courseID"];
                if (string.IsNullOrEmpty(courseIDParam))
                {
                    WriteErrorResponse(context,
                                       "Course file upload service requires a \"courseID\" parameter.");
                    return;
                }

                // Make sure the course ID is an integer and a valid course ID at that
                int courseID;
                if (!int.TryParse(courseIDParam, out courseID))
                {
                    WriteErrorResponse(context,
                                       "The course ID must be a valid integer value.");
                    return;
                }

                // There might be an optional "target_folder" parameter
                string targetFolderParam = context.Request.Params["target_folder"];

                // Try to get the current OSBLE user
                Models.Users.UserProfile up = OSBLE.Utility.OsbleAuthentication.CurrentUser;
                if (null == up)
                {
                    // In the future what I'd like to do here is look for a user name and
                    // password in the request headers. This would allow this web service to
                    // be used by other sources, but for now it requires a logged in OSBLE user.
                    WriteErrorResponse(context,
                                       "Could not get active OSBLE user for request. Please login.");
                    return;
                }

                // Make sure this user has permission to upload to this course
                OSBLEContext _db        = new OSBLEContext();
                CourseUser   courseUser = (
                    from cu in _db.CourseUsers
                    where cu.UserProfileID == up.ID
                    &&
                    cu.AbstractCourse is AbstractCourse
                    &&
                    cu.AbstractCourseID == courseID
                    select cu
                    ).FirstOrDefault();
                if (null == courseUser || !courseUser.AbstractRole.CanUploadFiles)
                {
                    // User cannot upload files for this course
                    context.Response.Write(
                        "<CourseFilesUploaderResponse success=\"false\">" +
                        "  <Message>The specified user does not have access to course with ID=" +
                        courseID.ToString() + ". User must be " +
                        "a course owner to access this service.</Message>" +
                        "</CourseFilesUploaderResponse>");
                    return;
                }

                // We will look for an optional "fileusage" parameter that tells us where
                // the file(s) will go. By default we use "generic" if the parameter is
                // absent.
                string fileUsage = context.Request.Params["fileusage"];
                if (string.IsNullOrEmpty(fileUsage))
                {
                    // Default to "generic", which puts files in the CourseDocs folder.
                    fileUsage = "generic";
                }
                else
                {
                    fileUsage = fileUsage.ToLower();
                }

                // Save based on the usage
                if ("generic" == fileUsage)
                {
                    OSBLEDirectory location = Models.FileSystem.Directories.GetCourseDocs(courseID);

                    // For now the target folder parameter is only allowed for generic files
                    if (!string.IsNullOrEmpty(targetFolderParam) &&
                        "\\" != targetFolderParam &&
                        "/" != targetFolderParam)
                    {
                        // We can't let it start with / or \
                        while (targetFolderParam.StartsWith("\\"))
                        {
                            targetFolderParam = targetFolderParam.Substring(1);
                        }
                        while (targetFolderParam.StartsWith("/"))
                        {
                            targetFolderParam = targetFolderParam.Substring(1);
                        }

                        location = location.GetDir(targetFolderParam);
                        if (null == location)
                        {
                            WriteErrorResponse(context,
                                               "Could not upload to target folder: " + targetFolderParam);
                            return;
                        }
                    }

                    // Save each file to the target directory
                    for (int i = 0; i < coll.Count; i++)
                    {
                        HttpPostedFile postedFile = coll[i];
                        //yc: check the file size in MB
                        int size = postedFile.ContentLength / 1024 / 1024; //contentLenght(BYTES)/ 1024 (gets KB) / 1024 (GETS MB)
                        if (size < 30)
                        {
                        }
                        else
                        {
                            //too large
                        }
                        string fileName = Path.GetFileName(postedFile.FileName);
                        location.AddFile(fileName, postedFile.InputStream);
                    }

                    context.Response.Write(string.Format(
                                               "<CourseFilesUploaderResponse success=\"true\">" +
                                               "  <Message>Successfully uploaded {0} files</Message>" +
                                               "</CourseFilesUploaderResponse>", coll.Count));
                    return;
                }
                else if ("assignment_description" == fileUsage ||
                         "assignment_solution" == fileUsage)
                {
                    // In this case we also need an assignment ID parameter
                    string aIDString = context.Request.Params["assignmentID"];
                    if (string.IsNullOrEmpty(aIDString))
                    {
                        WriteErrorResponse(context,
                                           "An \"assignmentID\" parameter is required when uploading a " +
                                           "file for an assignment " +
                                           (("assignment_description" == fileUsage) ? "description." : "solution."));
                        return;
                    }

                    int aID;
                    if (!int.TryParse(aIDString, out aID))
                    {
                        WriteErrorResponse(context,
                                           "The \"assignmentID\" parameter must be an integer value.");
                        return;
                    }

                    // Assignment must exist
                    Models.FileSystem.AssignmentFilePath afs =
                        Models.FileSystem.Directories.GetAssignment(courseID, aID);
                    if (null == afs)
                    {
                        WriteErrorResponse(context,
                                           "Could not get assignment file path for assignment: " + aIDString);
                        return;
                    }

                    // Get the attributable files storage for this assignment
                    OSBLE.Models.FileSystem.OSBLEDirectory attrFiles = afs.AttributableFiles;
                    if (null == attrFiles)
                    {
                        WriteErrorResponse(context,
                                           "Internal error: could not get attributable files manager for assignment");
                        return;
                    }

                    // Set up the system attributes for this file
                    Dictionary <string, string> sys = new Dictionary <string, string>();
                    sys.Add("created", DateTime.Now.ToString());
                    sys.Add(fileUsage, aIDString);
                    sys.Add("uploadedby", up.UserName);
                    if ("assignment_solution" != fileUsage)
                    {
                        sys.Add("any_course_user_can_download", null);
                    }

                    // Save each file to the target directory
                    for (int i = 0; i < coll.Count; i++)
                    {
                        HttpPostedFile postedFile = coll[i];
                        string         fileName   = Path.GetFileName(postedFile.FileName);
                        attrFiles.AddFile(fileName, postedFile.InputStream, sys, null);
                    }

                    context.Response.Write(string.Format(
                                               "<CourseFilesUploaderResponse success=\"true\">" +
                                               "  <Message>Successfully uploaded {0} files</Message>" +
                                               "</CourseFilesUploaderResponse>", coll.Count));
                    return;
                }

                // Coming here implies we didn't recognize the file usage
                WriteErrorResponse(context, "Unsupported file usage: " + fileUsage);
            }
            catch (HttpException ex)
            {
                if (ex.WebEventCode == 3004)
                {
                    WriteErrorResponse(context, "File exceeds 30MB upload limit");
                    return;
                }
            }
            // This web service returns XML
        }