private void ClearQueueTempFolder()
 {
     if (Directory.Exists(longFolderName))
     {
         foreach (var file in Directory.GetFiles(longFolderName))
         {
             File.SetAttributes(file, FileAttributes.Archive);
         }
         Directory.Delete(longFolderName, true);
     }
 }
        public void SetUp()
        {
            longFolderName = Path.Combine(Path.GetTempPath(), new String('a', 230));

            _blobId       = new BlobId(_originalFormat, 1);
            _pathToTask   = Path.Combine(longFolderName, "File_1.dsimport");
            _fileToImport = Path.Combine(longFolderName, "A Word Document.docx");
            _fileUri      = new Uri(Path.Combine(longFolderName, "A word document.docx"));

            ClearQueueTempFolder();
            Directory.CreateDirectory(longFolderName);

            File.Copy(Path.Combine(TestConfig.DocumentsFolder, "Queue\\File_1.dsimport"), _pathToTask);
            File.Copy(TestConfig.PathToWordDocument, _fileToImport);
            var accessor = Substitute.For <ITenantAccessor>();
            var tenant   = Substitute.For <ITenant>();

            tenant.Id.Returns(new TenantId("tests"));
            var container = Substitute.For <IWindsorContainer>();

            _commandBus = Substitute.For <ICommandBus>();
            var identityGenerator = Substitute.For <IIdentityGenerator>();

            _blobstore = Substitute.For <IBlobStore>();
            _blobstore.Upload(Arg.Is(_originalFormat), Arg.Any <string>()).Returns(_blobId);
            _blobstore.Upload(Arg.Is(_originalFormat), Arg.Any <FileNameWithExtension>(), Arg.Any <Stream>()).Returns(_blobId);

            accessor.GetTenant(_testTenant).Returns(tenant);
            accessor.Current.Returns(tenant);
            tenant.Container.Returns(container);

            container.Resolve <IBlobStore>().Returns(_blobstore);
            container.Resolve <IIdentityGenerator>().Returns(identityGenerator);
            container.Resolve <IMongoDatabase>().Returns(MongoDbTestConnectionProvider.ReadModelDb);
            var collection = MongoDbTestConnectionProvider.ReadModelDb.GetCollection <ImportFailure>("sys.importFailures");

            collection.Drop();
            DocumentStoreTestConfiguration config = new DocumentStoreTestConfiguration(tenantId: "tests");

            config.SetFolderToMonitor(longFolderName);
            var sysDb = config.TenantSettings.Single(t => t.TenantId == "tests").Get <IMongoDatabase>("system.db");

            sysDb.Drop();
            _queue = new ImportFormatFromFileQueue(config, accessor, _commandBus)
            {
                Logger = new ConsoleLogger()
            };

            _queue.DeleteTaskFileAfterImport = false;
        }
        public string Download(BlobId blobId, string folder)
        {
            if (blobId == null)
            {
                throw new ArgumentNullException(nameof(blobId));
            }

            if (String.IsNullOrEmpty(folder))
            {
                throw new ArgumentNullException(nameof(folder));
            }

            if (!Directory.Exists(folder))
            {
                throw new ArgumentException($"folder {folder} does not exists", nameof(folder));
            }

            var descriptor = _mongodDbFileSystemBlobDescriptorStorage.FindOneById(blobId);

            if (descriptor == null)
            {
                throw new ArgumentException($"Descriptor for {blobId} not found in {_mongodDbFileSystemBlobDescriptorStorage.GetType().Name}");
            }

            var localFileName = _directoryManager.GetFileNameFromBlobId(blobId, descriptor.FileNameWithExtension);

            if (!File.Exists(localFileName))
            {
                Logger.Error($"Blob {blobId} has descriptor, but blob file {localFileName} not found in the system.");
                throw new ArgumentException($"Blob {blobId} not found");
            }

            var    originalFileName    = descriptor.FileNameWithExtension.ToString();
            string destinationFileName = Path.Combine(folder, originalFileName);
            Int32  uniqueId            = 1;

            while (File.Exists(destinationFileName))
            {
                destinationFileName = Path.Combine(folder, Path.GetFileNameWithoutExtension(originalFileName) + $" ({uniqueId++})") + Path.GetExtension(originalFileName);
            }

            File.Copy(localFileName, destinationFileName);

            if (Logger.IsDebugEnabled)
            {
                Logger.Debug($"Blob {blobId} downloaded in folder {folder} with name {destinationFileName}");
            }
            return(destinationFileName);
        }
Ejemplo n.º 4
0
        private String GetRawFileNameFromBlobId(BlobId blobId, String fileName)
        {
            var           id            = blobId.Id;
            var           stringPadded  = String.Format("{0:D15}", id / 1000);
            StringBuilder directoryName = new StringBuilder(15);

            for (int i = 0; i < Math.Min(stringPadded.Length, 15); i++)
            {
                directoryName.Append(stringPadded[i]);
                if (i % _folderPrefixLength == (_folderPrefixLength - 1))
                {
                    directoryName.Append(System.IO.Path.DirectorySeparatorChar);
                }
            }
            var finalDirectory = Path.Combine(BaseDirectory, blobId.Format, directoryName.ToString());

            Directory.EnsureDirectory(finalDirectory);

            return(finalDirectory + id + "." + Path.GetFileName(fileName));
        }
        /// <summary>
        /// Create a series of subdirectories that avoid cluttering thousands
        /// of files inside the very same folder.
        /// The logic is the following, we want at most 1000 file in a folder, so
        /// we divide the id by 1000 and we pad to 15 number, then we subdivide
        /// the resulting number in blok by 4, each folder will contain at maximum
        /// 1000 folders or files.
        /// </summary>
        /// <param name="blobId"></param>
        /// <returns></returns>
        public String GetFileNameFromBlobId(BlobId blobId)
        {
            var           id            = blobId.Id;
            var           stringPadded  = String.Format("{0:D15}", id / 1000);
            StringBuilder directoryName = new StringBuilder(15);

            for (int i = 0; i < Math.Min(stringPadded.Length, 15); i++)
            {
                directoryName.Append(stringPadded[i]);
                if (i % 3 == 2)
                {
                    directoryName.Append(System.IO.Path.DirectorySeparatorChar);
                }
            }
            var finalDirectory = Path.Combine(_baseDirectory, blobId.Format, directoryName.ToString());

            Directory.EnsureDirectory(finalDirectory);

            return(finalDirectory + id + ".blob");
        }
Ejemplo n.º 6
0
 public DirectoryManager(String baseDirectory, Int32 folderPrefixLength)
 {
     BaseDirectory       = baseDirectory;
     _folderPrefixLength = folderPrefixLength;
     Directory.EnsureDirectory(baseDirectory);
 }
 public DirectoryManager(String baseDirectory)
 {
     _baseDirectory = baseDirectory;
     Directory.EnsureDirectory(baseDirectory);
 }