Esempio n. 1
0
        /// <inheritdoc />
        /// <exception cref="InvalidNameException">Throws if filename is invalid.</exception>
        /// <exception cref="EntryAlreadyExistsException">Throws if file already exists.</exception>
        /// <exception cref="DirectoryNotFoundException">Throws if parent directory is not found.</exception>
        /// <exception cref="DataIsNullException">Throws if data is empty.</exception>
        public void CreateFile(string fileName, byte[] data)
        {
            _transactionWrapper.BeginTransaction();

            _storageOperationLocker.LockEntry(fileName, () =>
            {
                if (!NameValid(fileName))
                {
                    throw new InvalidNameException(fileName);
                }

                if (ExistsInternal(fileName))
                {
                    throw new EntryAlreadyExistsException(fileName);
                }

                var parentDirectoryName = fileName.GetParentFullName();

                _storageOperationLocker.LockEntry(parentDirectoryName, () =>
                {
                    if (!DirectoryExistsInternal(parentDirectoryName))
                    {
                        throw new DirectoryNotFoundException(parentDirectoryName);
                    }

                    if (data is null)
                    {
                        throw new DataIsNullException(fileName);
                    }

                    var parentDirectory = _directoryRepository.Find(parentDirectoryName);
                    _fileRepository.Create(new FileEntry(fileName, parentDirectory.Id, data));
                });
            });

            _transactionWrapper.EndTransaction();
        }