コード例 #1
0
        public string StarterFilesXML(System.Guid guid)
        {
            try
            {
                DataSet     ds              = new DataSet();
                string      downloadRoot    = SharedSupport.AddBackSlashToDirectory(System.Web.HttpContext.Current.Request.MapPath(String.Empty, Constants.ASSIGNMENTMANAGER_DOWNLOAD_DIRECTORY, true));
                string      storageLocation = String.Empty;
                AssignmentM assign          = AssignmentM.Load(_assignmentID);
                storageLocation = SharedSupport.AddBackSlashToDirectory(assign.StorageDirectory + Constants.STARTER_PROJECT_PATH);

                //add unique guid to download path
                downloadRoot = downloadRoot + SharedSupport.AddBackSlashToDirectory(guid.ToString());

                DatabaseCall dbc = new DatabaseCall("Assignments_GetFiles", DBCallType.Select);
                dbc.AddParameter("@AssignmentID", _assignmentID);
                dbc.Fill(ds);

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string source      = String.Empty;
                    string destination = String.Empty;

                    string filename = ds.Tables[0].Rows[i]["FileName"].ToString().Trim();
                    filename    = filename.Replace(@"\", @"/");
                    source      = SharedSupport.RemoveIllegalFilePathCharacters(storageLocation + filename);
                    destination = SharedSupport.RemoveIllegalFilePathCharacters(downloadRoot + filename);
                    //check to see if destination directory exists
                    string destinationDir = destination.Substring(0, destination.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(destinationDir))
                    {
                        Directory.CreateDirectory(destinationDir);
                    }
                    //check if file is there
                    if (System.IO.File.Exists(source))
                    {
                        System.IO.File.Copy(source, destination, true);
                    }
                    else
                    {
                        throw new System.IO.FileNotFoundException(SharedSupport.GetLocalizedString("Assignment_UploadedFileNotFound"));
                    }

                    if (filename.StartsWith(@"/"))
                    {
                        ds.Tables[0].Rows[i]["FileName"] = guid + filename;
                    }
                    else
                    {
                        ds.Tables[0].Rows[i]["FileName"] = guid + @"/" + filename;
                    }
                }
                return(ds.GetXml());
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }
コード例 #2
0
        private void saveFileList(string xmlFileList, string guid)
        {
            System.Data.DataSet      ds        = new System.Data.DataSet();
            System.IO.StringReader   reader    = new System.IO.StringReader(xmlFileList);
            System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(reader);
            ds.ReadXml(xmlReader);

            AssignmentM assign    = AssignmentM.Load(_assignmentID);
            string      targetdir = SharedSupport.AddBackSlashToDirectory(assign.StorageDirectory + _userID);
            string      uploadDir = SharedSupport.AddBackSlashToDirectory(System.Web.HttpContext.Current.Request.MapPath(String.Empty, Constants.ASSIGNMENTMANAGER_UPLOAD_DIRECTORY, true)) +
                                    SharedSupport.AddBackSlashToDirectory(guid);

            try
            {
                if (Directory.Exists(targetdir))
                {
                    // If the directory exists, remove it so we are not left with extra files around.
                    Directory.Delete(targetdir, true);
                }
                Directory.CreateDirectory(targetdir);
            }
            catch
            {
            }

            //get max project size from settings table (in megabytes)
            Int64 maxProjectSize = new Int64();

            maxProjectSize = Convert.ToInt32(SharedSupport.GetSetting(Constants.MAX_PROJECT_SETTING)) * Constants.BytesInMegaByte;
            //create variable to track project size
            Int64 currentProjectSize = new Int64();

            currentProjectSize = 0;

            //Cycle through all uploaded files and save a record for each in the user assignment files table
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string filename = ds.Tables[0].Rows[i]["Filename"].ToString();
                //make sure the file name is not blank
                if (filename != null && filename != String.Empty)
                {
                    filename = filename.Replace("/", @"\");
                    if (filename.StartsWith(@"\"))
                    {
                        filename = filename.Remove(0, 1);
                    }
                    this.AddFile(filename);
                }
                else
                {
                    //Throw an error because the file name for the given record is blank
                    throw new ApplicationException(SharedSupport.GetLocalizedString("StudentAssignment_BlankFileName_Error"));
                }

                string uploadFilePath = uploadDir + filename;
                string targetFilePath = targetdir + filename;
                string fullTargetDir  = targetFilePath.Substring(0, targetFilePath.LastIndexOf(@"\"));
                if (!Directory.Exists(fullTargetDir))
                {
                    Directory.CreateDirectory(fullTargetDir);
                }
                if (!File.Exists(uploadFilePath))
                {
                    throw new ApplicationException(SharedSupport.GetLocalizedString("SharedSupport_InvalidFileLocation_Error"));
                }
                else
                {
                    //Get the size of the file and add it to other's to see if project exceeds
                    //    the maximum size limit held in the settings table
                    currentProjectSize += new FileInfo(uploadFilePath).Length;

                    if (currentProjectSize > maxProjectSize)
                    {
                        //delete all files
                        Directory.Delete(uploadDir, true);
                        Directory.Delete(targetdir, true);
                        throw new ApplicationException(SharedSupport.GetLocalizedString("StudentAssignment_ProjectTooLarge") + maxProjectSize.ToString() + SharedSupport.GetLocalizedString("StudentAssignment_Megabytes"));
                    }
                }
                File.Copy(uploadFilePath, targetFilePath, true);
            }
        }
コード例 #3
0
        public bool SubmitStarter(string xmlFileListing, string pathGuid)
        {
            if (this.IsValid)
            {
                bool submitSuccess = true;
                try
                {
                    System.Data.DataSet    dsXmlFileListing = new System.Data.DataSet();
                    System.IO.StringReader reader           = new System.IO.StringReader(xmlFileListing);
                    try
                    {
                        System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(reader);
                        dsXmlFileListing.ReadXml(xmlReader);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(SharedSupport.GetLocalizedString("UploadDownload_UnableToCopyToServer"), ex);
                    }

                    this.ClearStarter();

                    string uploadPath = SharedSupport.AddBackSlashToDirectory(System.Web.HttpContext.Current.Request.MapPath(String.Empty, Constants.ASSIGNMENTMANAGER_UPLOAD_DIRECTORY, true));
                    uploadPath += SharedSupport.AddBackSlashToDirectory(pathGuid);

                    string destinationPath = SharedSupport.AddBackSlashToDirectory(this.StorageDirectory + Constants.STARTER_PROJECT_PATH);

                    try
                    {
                        //Clear old directory
                        if (Directory.Exists(destinationPath))
                        {
                            Directory.Delete(destinationPath, true);
                        }
                        Directory.CreateDirectory(destinationPath);
                    }
                    catch
                    {
                    }

                    //Save all files and relative paths to assignmentfile table
                    for (int i = 0; i < dsXmlFileListing.Tables[0].Rows.Count; i++)
                    {
                        string filename = dsXmlFileListing.Tables[0].Rows[i]["FileName"].ToString();
                        if (filename.StartsWith(@"\") || filename.StartsWith(@"/"))
                        {
                            filename = filename.Remove(0, 1);
                        }
                        string sourceFile      = String.Empty;
                        string destinationFile = String.Empty;
                        sourceFile      = SharedSupport.RemoveIllegalFilePathCharacters(uploadPath + filename);
                        destinationFile = SharedSupport.RemoveIllegalFilePathCharacters(destinationPath + filename);
                        //check to make sure the target directory exists
                        string targetDirectory = destinationFile.Substring(0, destinationFile.LastIndexOf("\\"));
                        if (!Directory.Exists(targetDirectory))
                        {
                            Directory.CreateDirectory(targetDirectory);
                        }

                        //check if file is there
                        if (System.IO.File.Exists(sourceFile))
                        {
                            System.IO.File.Copy(sourceFile, destinationFile, true);
                        }
                        else
                        {
                            throw new System.IO.FileNotFoundException(SharedSupport.GetLocalizedString("Assignment_UploadedFileNotFound"));
                        }

                        this.AddFile(filename);
                    }
                    try
                    {
                        Directory.Delete(uploadPath, true);
                    }
                    catch
                    {
                    }

                    // Send new Starter project notice.
                    if (_sendNewProject && SharedSupport.UsingSmtp)
                    {
                        string[] AssignmentName = new string[] { _shortName };
                        string   subject        = SharedSupport.GetLocalizedString("Notification_UpdatedProjectSubject", AssignmentName);
                        string   body           = SharedSupport.GetLocalizedString("Notification_UpdatedProjectBody", AssignmentName);
                        MessageM.sendEmailMessageToCourse(subject, body, String.Empty, _courseId);
                    }
                }
                catch (Exception ex)
                {
                    SharedSupport.HandleError(ex);
                    submitSuccess = false;
                }

                if (submitSuccess)
                {
                    this.StarterProjectFlag = true;
                    this.Update();
                }
                return(submitSuccess);
            }
            else
            {
                return(false);
            }
        }