コード例 #1
0
        public async Task UpdateBookAsync(string userId, int bookId, BookUpdateDto model)
        {
            Book   book       = null;
            Stream bookStream = null;

            using (var transaction = await _context.Database.BeginTransactionAsync())
            {
                try
                {
                    book = await GetBookForUserAsync(userId, bookId);

                    if (!String.IsNullOrEmpty(model.Author) || !String.IsNullOrEmpty(model.Title) ||
                        (model.PublishingDate != null && model.PublishingDate.HasValue))
                    {
                        bookStream = await DownloadBookAsync(userId, bookId, book.FileName);

                        bookStream.Position = 0;
                        var mobiDocument = await MobiService.GetMobiDocument(bookStream);

                        var editedBookStream = await UpdateMobiMetadata(mobiDocument, model);

                        editedBookStream.Position = 0;
                        await _storageService.UploadBookAsync(editedBookStream, userId, bookId, book.FileName);

                        book.HasBeenEdited = true;
                        book.LastEditDate  = DateTime.Now.ToUniversalTime();
                    }
                    await UpdateBookDb(book, model);

                    transaction.Commit();
                }
                catch (DbException e)
                {
                    transaction.Rollback();
                    bookStream.Position = 0;
                    var mobiDocument = await MobiService.GetMobiDocument(bookStream);

                    var editedBookStream = await UpdateMobiMetadata(mobiDocument, new BookUpdateDto { Title = book.Title, Author = book.Author });

                    await _storageService.UploadBookAsync(editedBookStream, userId, book.Id, book.FileName);

                    _logger.LogCritical(e.ToString());
                    throw;
                }
            }
        }
コード例 #2
0
        private static async Task <MobiMetadaDto> GetMobiMetadataAsync(Stream stream)
        {
            MobiMetadaDto mobiMetada   = new MobiMetadaDto();
            var           mobiDocument = await MobiService.GetMobiDocument(stream);

            mobiMetada.Author         = mobiDocument.Author;
            mobiMetada.Title          = mobiDocument.Title;
            mobiMetada.PublishingDate = (mobiDocument.PublishingDate.HasValue) ?
                                        mobiDocument.PublishingDate?.ToUniversalTime() :
                                        null;
            mobiMetada.Isbn = mobiDocument.ISBN;

            var coverStream = mobiDocument.CoverExtractor.Extract();

            if (coverStream != null)
            {
                mobiMetada.CoverStream = coverStream;
            }

            return(mobiMetada);
        }