Пример #1
0
        public ConversionResultModel ConvertAndGetTargetFileName(BookSourceModel source, BookFormatEnum outputFormat,
            bool compress = true)
        {
            Guid id = ExtractBook(source.FileName, source.Body);
            if (outputFormat != DefaultBookFormat)
            {
                Convert(id, outputFormat);
                var sourceFile = GetTempFileName(id, DefaultBookFormat);
                if (File.Exists(sourceFile))
                    File.Delete(sourceFile);
            }

            var result = new ConversionResultModel
            {
                DownloadFileName = string.Format("{0}.{1}",
                    Transliterator.FromCyrillicToTransliteration(TextHelper.CleanCharacters(source.Name)),
                    outputFormat).ToLower(),
                FullFileName = GetTempFileName(id, outputFormat)
            };

            if (compress)
                result  = Compress(result);

            return result;
        }
Пример #2
0
        public ActionResult DeliverBook(Guid bookId, BookFormatEnum bookFormat, bool compress = true, string email = null)
        {
            var bookSource = DataService.GetBookSource(bookId, User.UserId, User.IsSuperUser);

            if (bookSource == null)
            {
                return(new HttpNotFoundResult());
            }
            try
            {
                var target = _convertService.ConvertAndGetTargetFileName(bookSource, bookFormat, compress);


                if (!string.IsNullOrEmpty(email))
                {
                    DataService.AddDownload((Guid)User.UserId, bookId, DownloadTypeEnum.Email);
                    _mailService.DeliverBookToEmail(email, bookSource.Name, target);
                    if (System.IO.File.Exists(target.DownloadFileName))
                    {
                        System.IO.File.Delete(target.DownloadFileName);
                    }
                    return(new HttpStatusCodeResult(HttpStatusCode.OK));
                }

                DataService.AddDownload((Guid)User.UserId, bookId, DownloadTypeEnum.File);
                return(new FilePathResult(target.FullFileName, System.Net.Mime.MediaTypeNames.Application.Octet)
                {
                    FileDownloadName = target.DownloadFileName
                });
            }
            catch (Exception ex)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Пример #3
0
 private void Convert(Guid id, BookFormatEnum outputFormat)
 {
     var info = new ProcessStartInfo(string.Format("\"{0}\"", Path.Combine(_settings.Path, ExternalAppName)))
     {
         UseShellExecute = true,
         Arguments = string.Format("\"{0}\" \"{1}\"", GetTempFileName(id, DefaultBookFormat), GetTempFileName(id, outputFormat)),
         RedirectStandardOutput = false,
         RedirectStandardError = false,
         RedirectStandardInput = false,
         CreateNoWindow = true,
     };
     var process = Process.Start(info);
     process.WaitForExit(1 * 60 * 1000);
 }
Пример #4
0
        private void Convert(Guid id, BookFormatEnum outputFormat)
        {
            var info = new ProcessStartInfo(string.Format("\"{0}\"", Path.Combine(_settings.Path, ExternalAppName)))
            {
                UseShellExecute        = true,
                Arguments              = string.Format("\"{0}\" \"{1}\"", GetTempFileName(id, DefaultBookFormat), GetTempFileName(id, outputFormat)),
                RedirectStandardOutput = false,
                RedirectStandardError  = false,
                RedirectStandardInput  = false,
                CreateNoWindow         = true,
            };
            var process = Process.Start(info);

            process.WaitForExit(1 * 60 * 1000);
        }
Пример #5
0
        public ActionResult DeliverBook(Guid bookId, BookFormatEnum bookFormat, bool compress = true, string email = null)
        {
            var bookSource = DataService.GetBookSource(bookId, User.UserId, User.IsSuperUser);

            if (bookSource == null)
                return new HttpNotFoundResult();
            try
            {
                var target = _convertService.ConvertAndGetTargetFileName(bookSource, bookFormat, compress);

                if (!string.IsNullOrEmpty(email))
                {
                    DataService.AddDownload((Guid)User.UserId, bookId, DownloadTypeEnum.Email);
                    _mailService.DeliverBookToEmail(email, bookSource.Name, target);
                    if (System.IO.File.Exists(target.DownloadFileName))
                        System.IO.File.Delete(target.DownloadFileName);
                    return new HttpStatusCodeResult(HttpStatusCode.OK);
                }

                DataService.AddDownload((Guid)User.UserId, bookId, DownloadTypeEnum.File);
                return new FilePathResult(target.FullFileName, System.Net.Mime.MediaTypeNames.Application.Octet)
                {
                    FileDownloadName = target.DownloadFileName
                };
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message);
            }
        }
Пример #6
0
        public ConversionResultModel ConvertAndGetTargetFileName(BookSourceModel source, BookFormatEnum outputFormat,
                                                                 bool compress = true)
        {
            Guid id = ExtractBook(source.FileName, source.Body);

            if (outputFormat != DefaultBookFormat)
            {
                Convert(id, outputFormat);
                var sourceFile = GetTempFileName(id, DefaultBookFormat);
                if (File.Exists(sourceFile))
                {
                    File.Delete(sourceFile);
                }
            }

            var result = new ConversionResultModel
            {
                DownloadFileName = string.Format("{0}.{1}",
                                                 Transliterator.FromCyrillicToTransliteration(TextHelper.CleanCharacters(source.Name)),
                                                 outputFormat).ToLower(),
                FullFileName = GetTempFileName(id, outputFormat)
            };

            if (compress)
            {
                result = Compress(result);
            }

            return(result);
        }
Пример #7
0
        private string GetTempFileName(Guid identifier, BookFormatEnum outputFormat)
        {
            var fileName = string.Format("knigoskop_{0}.{1}", identifier, outputFormat.ToString().ToLower());

            return(Path.Combine(Path.GetTempPath(), fileName));
        }
Пример #8
0
 private string GetTempFileName(Guid identifier, BookFormatEnum outputFormat)
 {
     var fileName = string.Format("knigoskop_{0}.{1}", identifier, outputFormat.ToString().ToLower());
     return Path.Combine(Path.GetTempPath(), fileName);
 }