示例#1
0
        /// <summary>
        /// Creates the fileNode for given directory
        /// </summary>
        /// <param name="source">directory info</param>
        /// <param name="ifolderID">ifolder id</param>
        /// <param name="parentID">parent id</param>
        /// <param name="entryName">entry name for ifolder</param>
        /// <returns>true if successful</returns>
        public static bool CreateFileNodesForDirectory(DirectoryInfo source, string ifolderID, string parentID, string entryName)
        {
            log.Debug("Creating nodes for directory: {0}", source.Name);
            Store store = Store.GetStore();

            // collection
            Collection c = store.GetCollectionByID(ifolderID);

            if (c == null)
            {
                throw new iFolderDoesNotExistException(ifolderID);
            }

            Node n = iFolderEntry.GetEntryByPath(c, entryName);

            if (n == null)
            {
                throw new Exception(string.Format("Node entry for {0} does not exist", source.FullName));
            }

            if (ifolderID == parentID)
            {
                iFolderEntry entry = iFolderEntry.GetEntry(c, c.GetRootDirectory());
                if (entry == null)
                {
                    throw new Exception(string.Format("Node entry for {0} does not exist", parentID));
                }
                parentID = entry.ID;
            }
            Node   parent = c.GetNodeByID(parentID);
            string path;

            FileInfo[]       files = source.GetFiles();
            iFolderEntryType type;

            foreach (FileInfo file in files)
            {
                log.Debug("CreateEntry for file name: {0}", file.Name);
                string tempEntryPath = entryName + "/" + file.Name;
                Node   n1            = iFolderEntry.GetEntryByPath(c, tempEntryPath);
                if (n1 != null)
                {
                    continue;
                }
                type = iFolderEntryType.File;
                try
                {
                    Node entry = CreateEntry(c, parent, type, file.Name, out path, true);
                    // update
                    (entry as FileNode).UpdateFileInfo(c);
                    c.Commit(entry);
                    // Create Hashmap also...
                    if (c.EncryptionAlgorithm == null || c.EncryptionAlgorithm == "")
                    {
                        // Upload hashmap for unencrypted iFolders..
                        FileNode node = new FileNode(entry);
                        HashMap  map  = new HashMap(c, (BaseFileNode)node);
                        map.CreateHashMapFile();
                    }
                }
                catch (Exception e)
                {
                    log.Debug("Exception In creating file entry for: {0}. Exception: {1}--{2}", file.FullName, e.Message, e.StackTrace);
                }
            }
            DirectoryInfo[] dirs = source.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                string tempEntryPath = entryName + "/" + dir.Name;
                Node   entry         = iFolderEntry.GetEntryByPath(c, tempEntryPath);
                if (entry == null)
                {
                    try
                    {
                        entry = CreateEntry(c, parent, iFolderEntryType.Directory, dir.Name, out path);
                        // create directory and node
                        // DirectoryInfo info = Directory.CreateDirectory(path);

                        // update
                        (entry as DirNode).CreationTime = dir.CreationTime;
                        c.Commit(entry);
                        CreateFileNodesForDirectory(dir, ifolderID, entry.ID, (entry as DirNode).GetRelativePath());
                    }
                    catch (Exception ex)
                    {
                        log.Debug("Exception in creating dir entry for: {0}", ex.Message);

                        /*
                         * if (Directory.Exists(path))
                         * {
                         *      Directory.Delete(path);
                         * }
                         * throw;
                         */
                    }
                }
                else
                {
                    CreateFileNodesForDirectory(dir, ifolderID, entry.ID, (entry as DirNode).GetRelativePath());
                }
            }
            return(true);
        }
示例#2
0
        /// <summary>
        /// Process the Request
        /// </summary>
        /// <param name="context">The HttpContext object.</param>
        public override void ProcessRequest(HttpContext context)
        {
            const int BUFFERSIZE = (16 * 1024);
            string    backupPath = null;

            try
            {
                bool DontCheckPolicies = false;
                // initialize
                Initialize(context);
                Length = int.Parse(context.Request.QueryString["Length"]);
                string dontCheckPolicies = null;
                long   NodeLength        = Length;
                try
                {
                    dontCheckPolicies = context.Request.QueryString["DontCheckPolicies"];
                    if (dontCheckPolicies != null && dontCheckPolicies == "true")
                    {
                        DontCheckPolicies = true;
                    }
                }
                catch
                {
                }

                try
                {
                    string nodelength = context.Request.QueryString["NodeLength"];
                    if (nodelength != null && nodelength != string.Empty)
                    {
                        Length = int.Parse(nodelength);
                    }
                }
                catch
                {
                }


                // does member have write rights
                if ((member.Rights != Access.Rights.Admin) && (member.Rights != Access.Rights.ReadWrite))
                {
                    throw new AccessException(collection, member, Access.Rights.ReadWrite);
                }

                long backupLength = 0;

                // new file?
                if (node == null)
                {
                    filename = System.IO.Path.GetFileName(entryPath);

                    Node parent = iFolderEntry.GetEntryByPath(collection,
                                                              System.IO.Path.GetDirectoryName(entryPath).Replace('\\', '/'));

                    node = (FileNode)iFolderEntry.CreateEntry(collection, parent,
                                                              iFolderEntryType.File, filename, out filePath, DontCheckPolicies);
                }
                else
                {
                    // check file type policy
                    FileTypeFilter fsFilter = FileTypeFilter.Get(collection);
                    if (!fsFilter.Allowed(filename))
                    {
                        throw new FileTypeException(filename);
                    }
                    // backup file
                    backupPath = String.Format("{0}.simias.temp", filePath);
                    File.Copy(filePath, backupPath, true);
                    backupLength = (new FileInfo(backupPath)).Length;
                }

                long deltaSize = context.Request.ContentLength - backupLength;

                if (DontCheckPolicies == false)
                {
                    if (collection.Disabled)
                    {
                        throw new LockException();
                    }
                    // Check first, if this file is violating aggregate disk quota limit set to his group
                    if (!iFolderUser.GroupQuotaUploadAllowed(collection.Owner.UserID, deltaSize))
                    {
                        throw new DiskQuotaException(filename);
                    }

                    // check file size policy
                    FileSizeFilter fsFilter = FileSizeFilter.Get(collection);
                    if (!fsFilter.Allowed(deltaSize))
                    {
                        throw new FileSizeException(filename);
                    }

                    // check disk quota policy
                    DiskSpaceQuota dsQuota = DiskSpaceQuota.Get(collection);
                    if (!dsQuota.Allowed(deltaSize))
                    {
                        throw new DiskQuotaException(filename);
                    }
                }
                try{
                    // lock the file
                    FileStream stream = File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.None);

                    // reader
                    Stream reader = context.Request.InputStream;

                    try
                    {
                        byte[] buffer = new byte[BUFFERSIZE];

                        int count = 0;

                        // download file
                        while ((count = reader.Read(buffer, 0, BUFFERSIZE)) > 0)
                        {
                            stream.Write(buffer, 0, count);
                            stream.Flush();
                        }
                    }
                    finally
                    {
                        // release the file
                        stream.Close();

                        // release the reader
                        reader.Close();
                    }


                    // update node
                    node.UpdateWebFileInfo(collection, Length);
                    //node.UpdateFileInfo(collection);
                    collection.Commit(node);

                    /*
                     *      As of now the hash map from web access is being uploaded only for unencrypted files. So for encrypted files, if we are doing delta sync, then the existing hashmap should be removed immediately after the file is uploaded ( because we are not uploading the new hashmap for the file.)
                     */
                    /* Uploading Hash map for unencrypted files */
                    if (collection.EncryptionAlgorithm == null || collection.EncryptionAlgorithm == "")
                    {
                        // Upload hashmap for unencrypted iFolders..
                        log.LogAccess("Upload hash", node.GetRelativePath(), node.ID, "Success");
                        HashMap map = new HashMap(collection, node);
                        map.CreateHashMapFile();
                    }

                    // log
                    log.LogAccess("Upload", node.GetRelativePath(), node.ID, "Success");
                }
                catch
                {
                    // restore backup
                    if (backupPath != null)
                    {
                        File.Copy(backupPath, filePath, true);
                    }

                    // log
                    log.LogAccess("Upload", node.GetRelativePath(), node.ID, "Failed");

                    throw;
                }
                finally
                {
                    // delete backup file
                    if ((backupPath != null) && File.Exists(backupPath))
                    {
                        File.Delete(backupPath);
                        backupPath = null;
                    }
                }
            }
            catch (Exception e)
            {
                // consume the file for better error reporting
                Stream reader = null;

                try
                {
                    // reader
                    reader = context.Request.InputStream;

                    byte[] buffer = new byte[BUFFERSIZE];

                    // download file
                    while (reader.Read(buffer, 0, BUFFERSIZE) > 0)
                    {
                        ;
                    }
                }
                catch
                {
                    // ignore
                }
                finally
                {
                    // release the reader
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }

                // create an HTTP error
                context.Response.StatusCode        = (int)HttpStatusCode.InternalServerError;
                context.Response.StatusDescription = e.GetType().Name;
            }
            finally
            {
                // delete backup file
                if ((backupPath != null) && File.Exists(backupPath))
                {
                    File.Delete(backupPath);
                    backupPath = null;
                }
            }
        }