Ejemplo n.º 1
0
        private T createVirtualEntity <T>(VirtualDirectory parentDirectory, string name)
            where T : BaseVirtualEntity
        {//todo: refactoring required
            Func <DirectoryEntry, Regex, IEnumerable <BaseEntry> > getInnerEntities;
            Func <long>      getNewEntryId;
            Func <BaseEntry> createEntry;
            Func <long, T>   getVirtualEntry;

            var parentDirectoryEntry = getDirectoryEntry(parentDirectory);
            var entryName            = VirtualPath.GetFileNameWithoutExtension(name);

            if (typeof(T) == typeof(VirtualFile))
            {
                getNewEntryId    = () => Indexer.GetNewFileId();
                getInnerEntities = (directoryEntry, searchString) => getDirectoryFiles(directoryEntry, false, searchString);
                createEntry      = () =>
                {
                    var extension = VirtualPath.GetFileExtension(name);
                    return(new FileEntry
                    {
                        Id = getNewEntryId(),
                        DirectoryId = parentDirectory.Id,
                        Name = entryName,
                        Extension = extension
                    });
                };
                getVirtualEntry = id => getVirtualFile(id) as T;
            }
            else if (typeof(T) == typeof(VirtualDirectory))
            {
                getNewEntryId    = () => Indexer.GetNewDirectoryId();
                getInnerEntities = (directoryEntry, searchString) => getNestedDirecories(directoryEntry, false, searchString);
                createEntry      = () => new DirectoryEntry
                {
                    Id          = getNewEntryId(),
                    DirectoryId = parentDirectory.Id,
                    Name        = entryName
                };
                getVirtualEntry = id => getVirtualDirectory(id) as T;
            }
            else
            {
                throw new InvalidOperationException();
            }

            var innerEntities = getInnerEntities(parentDirectoryEntry, makeRegexPattern(name));

            if (innerEntities.Any())
            {
                throw new InvalidOperationException($"Item with the same name exists in the directory {GetDirectoryName(parentDirectoryEntry.Id)}");
            }

            var newEntry = createEntry();

            _rawDataManager.Write(newEntry);
            Indexer.AddEntry(newEntry);
            var retv = getVirtualEntry(newEntry.Id);

            return(retv);
        }
Ejemplo n.º 2
0
        public void T6_GetFileExtensionTest()
        {
            var path      = "foo/bar/someFile.dat";
            var extension = VirtualPath.GetFileExtension(path);

            Assert.AreEqual("dat", extension);
        }
Ejemplo n.º 3
0
        public void RenameFile(VirtualFile file, string newName)
        {
            var fileEntry       = getFileEntry(file);
            var parentDirectory = getVirtualDirectory(fileEntry.DirectoryId);

            if (!_locker.CanWrite(fileEntry))
            {
                throw new AccessViolationException("Unable to rename file: file is locked");
            }

            var name = VirtualPath.GetFileNameWithoutExtension(newName);

            VirtualPath.CheckRestrictedSymbols(name);

            if (fileEntry.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
            {
                return;
            }

            Indexer.TryGetParentDirectory(fileEntry, out var parentDirectoryEntry);
            if (getDirectoryFiles(parentDirectoryEntry, false, makeRegexPattern(name)).Any())
            {
                throw new InvalidOperationException($"Item with the same name exists in the directory {GetDirectoryName(parentDirectoryEntry.Id)}");
            }

            using (_locker.LockWriting(fileEntry))
                using (_locker.LockReading(fileEntry))
                {
                    var newExtension = VirtualPath.GetFileExtension(newName);
                    fileEntry.ModificationTime = DateTime.Now;
                    fileEntry.Name             = name;
                    fileEntry.Extension        = newExtension;
                    Cache.FileNames.Remove(fileEntry.Id);
                    _rawDataManager.Write(fileEntry);
                }

            _directoryWatcherSource.RaiseUpdated(parentDirectory, file);
        }
Ejemplo n.º 4
0
        public async Task <VirtualFile> CopyFile(VirtualFile file, VirtualDirectory targetDirectory, Action <ProgressArgs> progressCallback, CancellationToken cancellationToken)
        {
            var fileEntry = getFileEntry(file);
            var filename  = VirtualPath.GetFileName(file.Name);//get filename from full path

            if (fileEntry.DirectoryId == targetDirectory.Id)
            {
                filename = $"Copy {filename}";
            }

            var idx = 0;

            while (targetDirectory.GetFiles(false, filename).Any())
            {
                var name = VirtualPath.GetFileNameWithoutExtension(filename);
                var ext  = VirtualPath.GetFileExtension(filename);
                filename = $"{name} ({++idx}).{ext}";
            }

            var createdFile = targetDirectory.CreateFile(filename);

            using (var newFileStream = createdFile.Open(FileMode.Open, FileAccess.Write))
                using (var sourceFileStream = file.Open(FileMode.Open, FileAccess.Read))
                {
                    newFileStream.SetLength(sourceFileStream.Length);
                    var restBytes       = sourceFileStream.Length;
                    var buffer          = BufferHelper.GetBuffer(sourceFileStream);
                    var currentProgress = -1;
                    while (restBytes > 0 && !cancellationToken.IsCancellationRequested)
                    {
                        try
                        {
                            var count = (int)(restBytes > buffer.Length ? buffer.Length : restBytes);
                            var read  = await sourceFileStream.ReadAsync(buffer, 0, count, cancellationToken);

                            await newFileStream.WriteAsync(buffer, 0, count, cancellationToken);

                            restBytes -= read;
                        }
                        catch (TaskCanceledException)
                        {
                        }

                        //report progress
                        var progress = sourceFileStream.GetProgress();
                        if (progress == currentProgress)
                        {
                            continue;
                        }

                        currentProgress = progress;
                        var message      = VirtualPath.GetFileName(createdFile.Name);
                        var progressArgs = new CopyProgressArgs(progress, message)
                        {
                            Operation = Operation.Copying
                        };
                        _synchronizationContext.Post(x => progressCallback?.Invoke((ProgressArgs)x), progressArgs);
                    }
                }

            if (!cancellationToken.IsCancellationRequested)
            {
                return(createdFile);
            }

            Remove(createdFile);
            _directoryWatcherSource.RaiseDeleted(targetDirectory, file);

            return(createdFile);
        }