Exemplo n.º 1
0
        public static void SynchronizeFolder( int PortalId, string physicalPath, string relativePath, bool isRecursive, bool syncFiles, bool forceFolderSync )
        {
            FolderController objFolderController = new FolderController();
            int FolderId = 0;
            bool isInSync = true;

            // synchronize folder collection
            if( forceFolderSync == true & relativePath == "" )
            {
                RemoveOrphanedFolders( PortalId );
            }

            //Attempt to get the folder
            FolderInfo folder = (FolderInfo)( CBO.FillObject( DataProvider.Instance().GetFolder( PortalId, relativePath ), typeof( FolderInfo ) ) );

            DirectoryInfo dirInfo = new DirectoryInfo( physicalPath );
            if( dirInfo.Exists )
            {
                // check to see if the folder exists in the db
                if( folder == null )
                {
                    // check if folder contains files or subfolders
                    if( dirInfo.GetFileSystemInfos( "*" ).Length != 0 )
                    {
                        //Add Folder to database
                        FolderId = AddFolder( PortalId, relativePath, (int)FolderController.StorageLocationTypes.InsecureFileSystem );
                        folder = objFolderController.GetFolderInfo( PortalId, FolderId );
                        isInSync = false;
                    }
                }
                else
                {
                    //Check whether existing folder is in sync by comparing LastWriteTime of the physical folder with the LastUpdated value in the database
                    //*NOTE: dirInfo.LastWriteTime is updated when files are added to or deleted from a directory - but NOT when existing files are overwritten ( this is a known Windows Operating System issue )
                    isInSync = ( dirInfo.LastWriteTime.ToString( "yyyyMMddhhmmss" ) == folder.LastUpdated.ToString( "yyyyMMddhhmmss" ) );
                }

                if( folder != null )
                {
                    if( syncFiles == true & ( isInSync == false || forceFolderSync == true ) )
                    {
                        //Get Physical Files in this Folder and sync them
                        string[] strFiles = Directory.GetFiles( physicalPath );
                        foreach( string strFileName in strFiles )
                        {
                            //Add the File if it doesn't exist, Update it if the file size has changed
                            AddFile( strFileName, PortalId, false, folder.FolderID );
                        }

                        //Removed orphaned files
                        RemoveOrphanedFiles( folder, PortalId );

                        //Update the folder with the LastWriteTime of the directory
                        folder.LastUpdated = dirInfo.LastWriteTime;
                        objFolderController.UpdateFolder( folder );
                    }

                    //Get Physical Sub Folders (and synchronize recursively)
                    if( isRecursive )
                    {
                        string[] strFolders = Directory.GetDirectories( physicalPath );
                        foreach( string strFolder in strFolders )
                        {
                            DirectoryInfo dir = new DirectoryInfo( strFolder );
                            string relPath = Null.NullString;
                            if( relativePath == "" )
                            {
                                relPath = dir.Name + "/";
                            }
                            else
                            {
                                relPath = relativePath;
                                if( !( relativePath.EndsWith( "/" ) ) )
                                {
                                    relPath = relPath + "/";
                                }
                                relPath = relPath + dir.Name + "/";
                            }
                            SynchronizeFolder( PortalId, strFolder, relPath, true, syncFiles, forceFolderSync );
                        }
                    }
                }
            }
            else // physical folder does not exist on file system
            {
                if( folder != null )
                {
                    // folder exists in DB
                    if( folder.StorageLocation != (int)FolderController.StorageLocationTypes.DatabaseSecure )
                    {
                        // remove files and folder from DB
                        RemoveOrphanedFiles( folder, PortalId );
                        objFolderController.DeleteFolder( PortalId, relativePath.Replace( "\\", "/" ) );
                    }
                }
            }
        }