コード例 #1
0
        /// <summary>
        /// Creates the specified catalog entry.
        /// </summary>
        /// <param name="stream">The stream of <see cref="CatalogEntryStream" /> type to create the entry from.</param>
        /// <returns>The newly created instance of <see cref="CatalogEntry" /> type.</returns>
        public async Task <CatalogEntry> CreateCatalogEntry(CatalogEntryStream stream)
        {
            CatalogEntry entry = stream.Entry;

            FileContract fileContract = await this.mapper.MapNewAsync <CatalogEntry, FileContract>(entry);

            CatalogContract catalogContract = null;

            using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, true))
            {
                IFileRepository fileRepository = scope.GetRepository <IFileRepository>();
                fileContract = await fileRepository.SaveAsync(fileContract);

                ICatalogRepository catalogRepository = scope.GetRepository <ICatalogRepository>();
                catalogContract = await catalogRepository.GetAsync(fileContract.DirectoryID);

                scope.Commit();
            }

            entry = await this.mapper.MapAsync(fileContract, entry);

            entry.Catalog = await this.mapper.MapAsync(catalogContract, entry.Catalog);

            return(entry);
        }
コード例 #2
0
        /// <summary>
        /// Creates the specified catalog entry.
        /// </summary>
        /// <param name="stream">The stream of <see cref="CatalogEntryStream" /> type to create the entry from.</param>
        /// <returns>The newly created instance of <see cref="CatalogEntry" /> type.</returns>
        public async Task <CatalogEntry> CreateCatalogEntry(CatalogEntryStream stream)
        {
            CatalogEntry entry = stream.Entry;

            FileDocument fileDocument = await this.mapper.MapNewAsync <CatalogEntry, FileDocument>(entry);

            CatalogDocument catalogDocument = null;

            using (IDocumentContextScope scope = this.dataContextScopeFactory.CreateDocumentContextScope(this.connectionStrings.DataAggregationDB))
            {
                IFileDocumentRepository fileRepository = scope.GetRepository <IFileDocumentRepository>();
                fileDocument = await fileRepository.SaveAsync(fileDocument);

                if ((entry.Catalog?.ID).HasValue)
                {
                    ICatalogDocumentRepository catalogRepository = scope.GetRepository <ICatalogDocumentRepository>();
                    catalogDocument = await catalogRepository.GetAsync(entry.Catalog.ID);
                }
            }

            entry = await this.mapper.MapAsync(fileDocument, entry);

            entry.Catalog = await this.mapper.MapAsync(catalogDocument, entry.Catalog);

            return(entry);
        }
コード例 #3
0
        /// <summary>
        /// Gets the catalog entry by the initial instance set.
        /// </summary>
        /// <param name="entry">The initial catalog entry set.</param>
        /// <param name="offset">The offset index.</param>
        /// <param name="length">The number of bytes from byte array to return.</param>
        /// <returns>
        /// The instance of <see cref="CatalogEntry" /> type.
        /// </returns>
        public async Task <CatalogEntryStream> GetCatalogEntryStream(CatalogEntry entry, int offset = 0, int length = 0)
        {
            entry = await this.GetCatalogEntry(entry);

            if (File.Exists(entry.Path))
            {
                lock (FileAccessSyncObject)
                {
                    if (File.Exists(entry.Path))
                    {
                        using (FileStream stream = new FileStream(entry.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            long count = length == 0 ? stream.Length : length;

                            byte[] buffer = new byte[count];
                            stream.Read(buffer, offset, (int)count);

                            CatalogEntryStream result = new CatalogEntryStream(entry, count);

                            result.Write(buffer, 0, (int)count);
                        }
                    }
                }
            }

            return(new CatalogEntryStream(entry, 0));
        }
コード例 #4
0
        /// <summary>
        /// Gets the catalog entry by the initial instance set.
        /// </summary>
        /// <param name="stream">The catalog entry stream to create from.</param>
        /// <returns>The instance of <see cref="CatalogEntry"/> type.</returns>
        public async Task <CatalogEntry> CreateCatalogEntry(CatalogEntryStream stream)
        {
            return(await Task.Run(() =>
            {
                stream.Entry.ValidatePath();

                stream.Entry.Path = stream.Entry.GeneratePath(true);
                if (!File.Exists(stream.Entry.Path))
                {
                    lock (FileAccessSyncObject)
                    {
                        if (!File.Exists(stream.Entry.Path))
                        {
                            using (FileStream fileStream = File.Create(stream.Entry.Path, 1024, FileOptions.WriteThrough))
                            {
                                stream.CopyTo(fileStream);
                            }
                        }
                    }
                }

                stream.Entry.Size = new FileInfo(stream.Entry.Path).Length;

                return stream.Entry;
            }));
        }
コード例 #5
0
        /// <summary>
        /// Creates the specified catalog entry.
        /// </summary>
        /// <param name="stream">The <see cref="T:HomeCloud.DataStorage.Business.Entities.CatalogEntryStream" /> content stream of catalog entry.</param>
        /// <returns>
        /// The operation result containing created instance of <see cref="T:HomeCloud.DataStorage.Business.Entities.CatalogEntry" />.
        /// </returns>
        public async Task <ServiceResult <CatalogEntry> > CreateEntryAsync(CatalogEntryStream stream)
        {
            stream.Entry.ID = Guid.Empty;

            IServiceFactory <ICatalogEntryValidator> validator = this.validationServiceFactory.GetFactory <ICatalogEntryValidator>();

            ValidationResult result = await validator.Get <IRequiredValidator>().ValidateAsync(stream.Entry);

            result += await validator.Get <IUniqueValidator>().ValidateAsync(stream.Entry);

            if (!result.IsValid)
            {
                return(new ServiceResult <CatalogEntry>(stream.Entry)
                {
                    Errors = result.Errors
                });
            }

            Func <IDataProvider, Task> createCatalogEntryFunction = async provider => stream.Entry = await provider.CreateCatalogEntry(stream);

            Func <IDataProvider, Task> createCatalogEntryUndoFunction = async provider => stream.Entry = await provider.DeleteCatalogEntry(stream.Entry);

            this.processor.CreateDataHandler <IDataCommandHandler>().CreateAsyncCommand <IDataStoreProvider>(createCatalogEntryFunction, createCatalogEntryUndoFunction);
            this.processor.CreateDataHandler <IDataCommandHandler>().CreateAsyncCommand <IAggregationDataProvider>(async provider => stream.Entry.Catalog = await provider.GetCatalog(stream.Entry.Catalog), null);
            this.processor.CreateDataHandler <IDataCommandHandler>().CreateAsyncCommand <IFileSystemProvider>(createCatalogEntryFunction, createCatalogEntryUndoFunction);
            this.processor.CreateDataHandler <IDataCommandHandler>().CreateAsyncCommand <IAggregationDataProvider>(createCatalogEntryFunction, createCatalogEntryUndoFunction);

            await this.processor.ProcessAsync();

            return(new ServiceResult <CatalogEntry>(stream.Entry));
        }
コード例 #6
0
        /// <summary>
        /// Gets the content stream of the catalog entry by specified entry identifier.
        /// </summary>
        /// <param name="id">The catalog entry identifier.</param>
        /// <param name="offset">The offset index.</param>
        /// <param name="length">The number of bytes from byte array to return.</param>
        /// <returns>
        /// The operation result containing the instance of <see cref="CatalogEntryStream" />.
        /// </returns>
        public async Task <ServiceResult <CatalogEntryStream> > GetEntryStreamAsync(Guid id, int offset, int length = 1024)
        {
            CatalogEntry entry = new CatalogEntry()
            {
                ID = id
            };

            IServiceFactory <ICatalogEntryValidator> validator = this.validationServiceFactory.GetFactory <ICatalogEntryValidator>();
            ValidationResult result = await validator.Get <IPresenceValidator>().ValidateAsync(entry);

            if (!result.IsValid)
            {
                return(new ServiceResult <CatalogEntryStream>(new CatalogEntryStream(entry, 0))
                {
                    Errors = result.Errors
                });
            }

            CatalogEntryStream stream = null;

            Func <IDataProvider, Task> getCatalogEntryStreamFunction = async provider => entry = await provider.GetCatalogEntry(entry);

            this.processor.CreateDataHandler <IDataCommandHandler>().CreateAsyncCommand <IAggregationDataProvider>(async provider => entry = await provider.GetCatalogEntry(entry), null);
            this.processor.CreateDataHandler <IDataCommandHandler>()
            .CreateAsyncCommand <IDataStoreProvider>(async provider => entry   = await provider.GetCatalogEntry(entry), null)
            .CreateAsyncCommand <IFileSystemProvider>(async provider => stream = await provider.GetCatalogEntryStream(entry, offset, length), null);

            await this.processor.ProcessAsync();

            return(new ServiceResult <CatalogEntryStream>(stream));
        }
コード例 #7
0
        public async Task <IActionResult> Post(Guid catalogID, [FromBody] FileStreamViewModel model)
        {
            return(await this.HttpPost(
                       model,
                       async() =>
            {
                CatalogEntry entry = await this.Mapper.MapNewAsync <FileViewModel, CatalogEntry>(model);
                entry.Catalog.ID = catalogID;

                CatalogEntryStream stream = new CatalogEntryStream(entry, model.Stream);

                ServiceResult <CatalogEntry> result = await this.catalogEntryService.CreateEntryAsync(stream);

                return await this.HttpPostResult <CatalogEntry, DataViewModel>(this.Get, result);
            }));
        }