Esempio n. 1
0
        private async Task ProcessScanPath(int taskID, FileConnection Connection, IConnectionRoutedCacheManager connectionManager, string fileEntry)
        {
            var taskInfo = $"task: {taskID} connection: {Connection.name}";

            _logger.Log(LogLevel.Debug, $"{taskInfo} fileEntry: {fileEntry}");

            //Expand home dir syntax ex: ~/blah
            string filestr = _fileExpanderService.Expand(fileEntry);

            _logger.Log(LogLevel.Debug, $"{taskInfo} expanded: {filestr}");

            // get the file attributes for file or directory
            //            FileAttributes attr = File.GetAttributes(fileEntry);

            string directory = filestr;

            _logger.Log(LogLevel.Debug, $"{taskInfo} create directory: {directory}");

            var directoryFullPath = Path.GetDirectoryName(directory);

            Directory.CreateDirectory(directoryFullPath); //This may cause problems if we are specifying files instead of directories.
            Directory.CreateDirectory(directory);         //This may cause problems if we are specifying files instead of directories.

            string searchPattern;

            if (filestr.IndexOf("*") != -1)
            {
                _logger.Log(LogLevel.Debug, $"{taskInfo} wildcard search pattern detected.");

                directory     = Path.GetDirectoryName(filestr);
                searchPattern = Path.GetFileName(filestr);
            }
            else
            {
                _logger.Log(LogLevel.Debug, $"{taskInfo} using search pattern *.");
                searchPattern = "*";
            }

            FileAttributes attr = File.GetAttributes(directory);

            if (!attr.HasFlag(FileAttributes.Directory))
            {
                _logger.Log(LogLevel.Debug, $"{taskInfo} Sending individually specified {filestr}.");
                await _sendFileService.SendFile(filestr, taskID, Connection, connectionManager);

                return;
            }

            _logger.Log(LogLevel.Debug, $"{taskInfo} This is a directory.");

            var fileEntries = _util.DirSearch(directory, searchPattern);

            if (_profileStorage.Current.duplicatesDetectionUpload)
            {
                _logger.Log(LogLevel.Information,
                            $"{taskInfo} toCloud: {(fileEntries == null ? 0 : fileEntries.Count)} files to be send to cloud (before duplicates elimination).");
            }
            else
            {
                _logger.Log(LogLevel.Information,
                            $"{taskInfo} toCloud: {(fileEntries == null ? 0 : fileEntries.Count)} files to be send to cloud.");
            }

            foreach (string file in fileEntries)
            {
                _logger.Log(LogLevel.Debug, $"{taskInfo} Found {file}.");

                try
                {
                    attr = File.GetAttributes(file);
                    if (!attr.HasFlag(FileAttributes.Hidden))
                    {
                        var newTaskID = _taskManager.NewTaskID();
                        await _sendFileService.SendFile(file, newTaskID, Connection, connectionManager);
                    }
                    else
                    {
                        _logger.Log(LogLevel.Debug, $"{taskInfo} Deleting hidden file {file}.");
                        File.Delete(file); //delete hidden files like .DS_Store
                    }
                }
                catch (FileNotFoundException e)
                {
                    _logger.Log(LogLevel.Debug, $"Retrieve Exception: {e.Message} {e.StackTrace}");
                    //eat it if not found, it's already gone
                }
            }

            //cleanup empty directories.
            _logger.Log(LogLevel.Debug, $"{taskInfo} Cleaning Empty Directories {directory}.");

            _util.CleanUpDirectory(directory);
        }
Esempio n. 2
0
        public async Task SendFile(string filePath, int taskID, FileConnection connection, IConnectionRoutedCacheManager connectionManager)
        {
            Connection = connection;
            _logger.Log(LogLevel.Debug, $"Sending {filePath}");

            RoutedItem routedItem = new RoutedItem(fromConnection: Connection.name, sourceFileName: filePath, taskID: taskID)
            {
                type = RoutedItem.Type.FILE
            };

            try
            {
                routedItem.type = RoutedItem.Type.FILE;
                _routedItemManager.Init(routedItem);
                _routedItemManager.Enqueue(Connection, Connection.toRules, nameof(Connection.toRules));

                await _rulesManager.SendToRules(routedItem, _routedItemManager, connectionManager);

                //await LITE.profile.rules.SendToRules(routedItem);

                _routedItemManager.Dequeue(Connection, Connection.toRules, nameof(Connection.toRules));

                if (Connection.outpath != null && File.Exists(filePath))
                {
                    var expandedOutPath = _fileExpanderService.Expand(Connection.outpath);
                    // collapse the duplicate part of the path, prob more simple way of thinking about this.
                    string dir =
                        $"{expandedOutPath}{filePath.Substring(0, filePath.LastIndexOf(Path.DirectorySeparatorChar)).Replace(_fileExpanderService.Expand("~"), "")}";
                    string file = $"{expandedOutPath}{filePath.Replace(_fileExpanderService.Expand("~"), "")}";

                    _logger.Log(LogLevel.Debug, $"Moving {filePath} to {file}");
                    Directory.CreateDirectory(dir);

                    //await Task.Yield();

                    if (File.Exists(file))
                    {
                        var orgFileDateTime = File.GetLastWriteTime(file).ToString()
                                              .Replace(Path.DirectorySeparatorChar, '-').Replace(":", "-");
                        var destinationBackupFileName = $"{file}.{orgFileDateTime}";

                        // Remove the file to guarantee the move works
                        //File.Delete(file);
                        File.Replace(filePath, file, destinationBackupFileName, true);
                    }
                    else
                    {
                        File.Move(filePath, file);
                    }
                    _logger.Log(LogLevel.Debug, $"Moved {filePath} to {file}");
                }
                else
                {
                    _logger.Log(LogLevel.Debug, $"outpath is null.");
                }
            }
            catch (TaskCanceledException)
            {
                _logger.Log(LogLevel.Information, $"Task was canceled.");
            }
            catch (Exception e)
            {
                _logger.LogFullException(e);

                _routedItemManager.Init(routedItem);
                _routedItemManager.Dequeue(Connection, Connection.toRules, nameof(Connection.toRules), error: true);
            }
        }