Пример #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
        /// <summary>
        /// Renames a file and its corresponding attribute file. The existing
        /// file name must be just a file name with no subdirs and not an
        /// absolute (rooted) path. The same goes for the new file name.
        /// </summary>
        public bool RenameFile(string fileName, string newFileName)
        {
            // Quick security check
            if (fileName.Contains('/') || fileName.Contains('\\') ||
                newFileName.Contains('/') || newFileName.Contains('\\'))
            {
                return(false);
            }

            // First rename data file
            string dataFile    = Path.Combine(m_dataDir, fileName);
            string dataFileNew = Path.Combine(m_dataDir, newFileName);

            if (!System.IO.File.Exists(dataFile))
            {
                return(false);
            }
            System.IO.File.Move(dataFile, dataFileNew);

            // Now the attribute file
            string attrFile = AttributableFileCollection.GetAttrFileName(
                m_attrDir, fileName);
            string attrFileNew = AttributableFileCollection.GetAttrFileName(
                m_attrDir, newFileName);

            if (System.IO.File.Exists(attrFile))
            {
                System.IO.File.Move(attrFile, attrFileNew);
            }

            return(true);
        }
Пример #3
0
        public bool DeleteFile(string localFileName)
        {
            if (localFileName.Contains("../") || localFileName.Contains("..\\"))
            {
                // We won't allow going up a directory
                return(false);
            }

            string fullFileName = Path.Combine(m_dataDir, localFileName);

            if (!System.IO.File.Exists(fullFileName))
            {
                return(false);
            }

            // Delete the data file
            System.IO.File.Delete(fullFileName);

            // Delete the attribute file too, if it exists
            fullFileName = AttributableFileCollection.GetAttrFileName(
                m_attrDir, localFileName);
            if (System.IO.File.Exists(fullFileName))
            {
                System.IO.File.Delete(fullFileName);
            }

            return(true);
        }
Пример #4
0
        private FileCollection GetFilesWithAttribute(string attrClass, string attrName, string attrValue)
        {
            if (!System.IO.Directory.Exists(DataFilesPath))
            {
                return(new AttributableFileCollection(DataFilesPath, AttrFilesPath,
                                                      new List <string>()));
            }

            // Compare everything in lower case except the value
            attrClass = attrClass.ToLower();
            attrName  = attrName.ToLower();

            string[]      dataFiles = System.IO.Directory.GetFiles(DataFilesPath);
            List <string> files     = new List <string>();

            foreach (string file in dataFiles)
            {
                // Get the name for the attribute file
                string attrFileName = AttributableFileCollection.GetAttrFileName(AttrFilesPath, file);
                if (string.IsNullOrEmpty(attrFileName) ||
                    !System.IO.File.Exists(attrFileName))
                {
                    continue;
                }

                OSBLEFile af = OSBLEFile.CreateFromExisting(file, attrFileName);

                if ("systemattributes" == attrClass)
                {
                    if (!af.ContainsSysAttr(attrName, attrValue))
                    {
                        continue;
                    }
                }
                else
                {
                    if (!af.ContainsUserAttr(attrName, attrValue))
                    {
                        continue;
                    }
                }

                files.Add(file);
            }
            return(new AttributableFileCollection(DataFilesPath, AttrFilesPath, files));
        }
Пример #5
0
 public OSBLEFile GetFile(string fileName)
 {
     // If the file doesn't exist then we'll assume it's a relative path and
     // try combining it with the data files path.
     if (!System.IO.File.Exists(fileName))
     {
         fileName = Path.Combine(DataFilesPath, fileName);
         if (!System.IO.File.Exists(fileName))
         {
             return(null);
         }
     }
     return(OSBLEFile.CreateFromExisting(
                fileName,
                AttributableFileCollection.GetAttrFileName(
                    AttrFilesPath, Path.GetFileName(fileName))));
 }
Пример #6
0
        private void GetXMLListing(CourseUser courseUser, string dir, bool recurse, StringBuilder sb)
        {
            // Determine what permissions this user has, since permission attributes
            // will be put in the listing.
            bool canDeleteFolders;

            if (null == courseUser)
            {
                canDeleteFolders = false;
            }
            else
            {
                canDeleteFolders = courseUser.AbstractRole.CanModify;
            }

            string[] dataFiles = System.IO.Directory.GetFiles(dir);

            // Do directories first if we've been asked to recurse
            if (recurse)
            {
                foreach (string folder in System.IO.Directory.GetDirectories(dir))
                {
                    sb.AppendFormat("<folder name=\"{0}\" can_delete=\"{1}\" can_upload_to=\"{1}\">",
                                    folder.Substring(folder.LastIndexOf(Path.DirectorySeparatorChar) + 1),
                                    canDeleteFolders.ToString());
                    GetXMLListing(courseUser, folder, recurse, sb);
                    sb.Append("</folder>");
                }
            }

            // Determine the directory for the attribute XML files
            if (!dir.StartsWith(m_dataDir))
            {
                return;
            }
            string attrDir = m_attrDir + dir.Substring(m_dataDir.Length);

            // Now do the actual files
            foreach (string file in dataFiles)
            {
                // Get the file name for the attribute file
                string attrFileName = AttributableFileCollection.GetAttrFileName(attrDir, file);
                if (string.IsNullOrEmpty(attrFileName) ||
                    !System.IO.File.Exists(attrFileName))
                {
                    // Add a file with no attributes
                    sb.AppendFormat("<file name=\"{0}\" can_delete=\"{1}\"></file>",
                                    System.IO.Path.GetFileName(file), canDeleteFolders);
                }
                else
                {
                    // Add a file with attributes from its attribute XML file
                    string xml = System.IO.File.ReadAllText(attrFileName);
                    if (xml.StartsWith("<?xml"))
                    {
                        int i = xml.IndexOf("?>");
                        xml = xml.Substring(i + 2);
                    }
                    sb.AppendFormat("<file name=\"{0}\" can_delete=\"{2}\">{1}</file>",
                                    System.IO.Path.GetFileName(file), xml, canDeleteFolders);
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Adds a file, along with its attributes. If the file already exists then it will
        /// be overwritten and its attributes will be replaced.
        /// </summary>
        /// <param name="sysAttrs">Collection of system attributes to be associated with the
        /// file. This can be null if desired to create the file with an empty list of
        /// system attributes.</param>
        /// /// <param name="usrAttrs">Collection of user attributes to be associated with the
        /// file. This can be null if desired to create the file with an empty list of
        /// user attributes.</param>
        public bool AddFile(string fileName, Stream data,
                            Dictionary <string, string> sysAttrs,
                            Dictionary <string, string> usrAttrs)
        {
            // We don't allow subdirectories or absolute paths. We need just a file
            // name with no slashes.
            if (fileName.Contains('\\') || fileName.Contains('/'))
            {
                return(false);
            }

            string path     = DataFilesPath;
            string filePath = Path.Combine(path, fileName);

            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            if (!System.IO.Directory.Exists(AttrFilesPath))
            {
                System.IO.Directory.CreateDirectory(AttrFilesPath);
            }

            // Get the file name for the attributes file
            string attrFileName = AttributableFileCollection.GetAttrFileName(AttrFilesPath, fileName);

            // Create the XML file for the attributes
            XmlWriterSettings settings = new XmlWriterSettings()
            {
                CloseOutput  = true,
                Indent       = true,
                NewLineChars = "\n",
            };
            XmlWriter writer = null;

            try { writer = XmlWriter.Create(attrFileName, settings); }
            catch (Exception)
            {
                writer = null;
            }
            if (null == writer)
            {
                return(false);
            }

            // Write the attribute data
            writer.WriteStartElement("osblefileattributes");
            // System attributes first
            writer.WriteStartElement("systemattributes");
            WriteElements(writer, sysAttrs);
            writer.WriteEndElement();
            // Then user attributes
            writer.WriteStartElement("userattributes");
            WriteElements(writer, usrAttrs);
            writer.WriteEndElement();
            writer.WriteEndElement();
            // Close
            writer.Close();

            bool retVal = true;

            try
            {
                FileStream output = System.IO.File.Open(filePath, FileMode.Create);
                data.CopyTo(output);
                output.Close();
                output.Dispose();
            }
            catch (Exception)
            {
                retVal = false;
            }

            // If we wrote the attribute data but not the file data then we want
            // to delete the attribute file
            if (!retVal)
            {
                if (System.IO.File.Exists(attrFileName))
                {
                    System.IO.File.Delete(attrFileName);
                }
            }

            return(retVal);
        }