/// <inheritdoc/>
        public VirtualDrive(string license, string userFileSystemRootPath, Settings settings, ILog log)
            : base(license, userFileSystemRootPath, settings, log)
        {
            string remoteStorageRootPath = Mapping.MapPath(userFileSystemRootPath);

            remoteStorageMonitor = new RemoteStorageMonitor(remoteStorageRootPath, this, log);
        }
        /// <summary>
        /// Creates a vitual file system Engine.
        /// </summary>
        /// <param name="license">A license string.</param>
        /// <param name="userFileSystemRootPath">
        /// A root folder of your user file system.
        /// Your file system tree will be located under this folder.
        /// </param>
        /// <param name="serverDataFolderPath">Path to the folder that stores custom data associated with files and folders.</param>
        /// <param name="iconsFolderPath">Path to the icons folder.</param>
        /// <param name="log">Logger.</param>
        public VirtualEngine(string license, string userFileSystemRootPath, string serverDataFolderPath, string iconsFolderPath, ILog log)
            : base(license, userFileSystemRootPath)
        {
            logger = new Logger("File System Engine", log) ?? throw new NullReferenceException(nameof(log));
            this.serverDataFolderPath = serverDataFolderPath ?? throw new NullReferenceException(nameof(serverDataFolderPath));
            this.iconsFolderPath      = iconsFolderPath ?? throw new NullReferenceException(nameof(iconsFolderPath));

            // We want our file system to run regardless of any errors.
            // If any request to file system fails in user code or in Engine itself we continue processing.
            ThrowExceptions = false;

            StateChanged += Engine_StateChanged;
            Error        += Engine_Error;
            Message      += Engine_Message;

            string remoteStorageRootPath = Mapping.MapPath(userFileSystemRootPath);

            RemoteStorageMonitor = new RemoteStorageMonitor(remoteStorageRootPath, this, log);
            msOfficeDocsMonitor  = new MsOfficeDocsMonitor(userFileSystemRootPath, this, log);
        }
Beispiel #3
0
        //[STAThread]
        static async Task <int> Main(string[] args)
        {
            // Load Settings.
            IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, true).Build();

            Settings        = configuration.ReadSettings();
            Config.Settings = Settings;

            // Load Log4Net for net configuration.
            var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());

            XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

            // Enable UTF8 for Console window.
            Console.OutputEncoding = System.Text.Encoding.UTF8;

            log.Info($"\n{Settings.ProductName}");
            log.Info("\nPress 'Q' to unregister file system, delete all files/folders and exit (simulate uninstall with full cleanup).");
            log.Info("\nPress 'q' to unregister file system and exit (simulate uninstall).");
            log.Info("\nPress any other key to exit without unregistering (simulate reboot).");
            log.Info("\n----------------------\n");

            // Typically you will register sync root during your application installation.
            // Here we register it during first program start for the sake of the development convenience.
            if (!await Registrar.IsRegisteredAsync(Settings.UserFileSystemRootPath))
            {
                log.Info($"\nRegistering {Settings.UserFileSystemRootPath} sync root.");
                Directory.CreateDirectory(Settings.UserFileSystemRootPath);
                Directory.CreateDirectory(Settings.ServerDataFolderPath);

                await Registrar.RegisterAsync(SyncRootId, Settings.UserFileSystemRootPath, Settings.ProductName,
                                              Path.Combine(Config.Settings.IconsFolderPath, "Drive.ico"));
            }
            else
            {
                log.Info($"\n{Settings.UserFileSystemRootPath} sync root already registered.");
            }

            // Log indexed state. Indexing must be enabled for the sync root to function.
            StorageFolder userFileSystemRootFolder = await StorageFolder.GetFolderFromPathAsync(Settings.UserFileSystemRootPath);

            log.Info($"\nIndexed state: {(await userFileSystemRootFolder.GetIndexedStateAsync())}\n");

            ConfigureWebDAVClient();

            ConsoleKeyInfo exitKey = new ConsoleKeyInfo();

            // Event to be fired when any key will be pressed in the console or when the tray application exits.
            ManualResetEvent exitEvent = new ManualResetEvent(false);

            try
            {
                virtualDrive = new VirtualDrive(Settings.UserFileSystemLicense, Settings.UserFileSystemRootPath, log, Settings.SyncIntervalMs);
                RemoteStorageMonitorInstance = new RemoteStorageMonitor(Settings.WebDAVServerUrl, log);

                // Start tray application.
                Thread tryIconThread = WindowsTrayInterface.CreateTrayInterface(Settings.ProductName, virtualDrive.SyncService, exitEvent);

                // Start processing OS file system calls.
                //engine.ChangesProcessingEnabled = false;
                await virtualDrive.StartAsync();

                // Start monitoring changes in remote file system.
                //await RemoteStorageMonitorInstance.StartAsync();

#if DEBUG
                // Opens Windows File Manager with user file system folder and remote storage folder.
                ShowTestEnvironment();
#endif
                // Keep this application running until user input.
                exitKey = WebDAVDrive.UI.ConsoleManager.WaitConsoleReadKey(exitEvent);

                //wait until the button "Exit" is pressed or any key in console is peressed  to stop application
                exitEvent.WaitOne();
            }
            finally
            {
                virtualDrive.Dispose();
                RemoteStorageMonitorInstance.Dispose();
            }

            if (exitKey.KeyChar == 'q')
            {
                // Unregister during programm uninstall.
                await Registrar.UnregisterAsync(SyncRootId);

                log.Info($"\n\nUnregistering {Settings.UserFileSystemRootPath} sync root.");
                log.Info("\nAll empty file and folder placeholders are deleted. Hydrated placeholders are converted to regular files / folders.\n");
            }
            else if (exitKey.KeyChar == 'Q')
            {
                log.Info($"\n\nUnregistering {Settings.UserFileSystemRootPath} sync root.");
                log.Info("\nAll files and folders placeholders are deleted.\n");

                // Unregister during programm uninstall and delete all files/folder.
                await Registrar.UnregisterAsync(SyncRootId);

                try
                {
                    Directory.Delete(Settings.UserFileSystemRootPath, true);
                }
                catch (Exception ex)
                {
                    log.Error($"\n{ex}");
                }

                try
                {
                    Directory.Delete(Config.Settings.ServerDataFolderPath, true);
                }
                catch (Exception ex)
                {
                    log.Error($"\n{ex}");
                }
            }
            else
            {
                log.Info("\n\nAll downloaded file / folder placeholders remain in file system. Restart the application to continue managing files.\n");
            }

            return(1);
        }