public void AsciiConverterTest(Color color, string expected) { var imageConverter = new Ascii(1); var imageProvider = new MockImageProvider( color ); var fileConverter = new FileConverter( imageConverter, imageProvider ); var result = fileConverter.ConvertFile( "dummy.png" ); Assert.That( result, Is.EqualTo( expected + "\r\n" ) ); }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { Attachment attachment; HttpRequestBase httpRequestBase = controllerContext.RequestContext.HttpContext.Request; if (!String.IsNullOrEmpty(httpRequestBase["qqfile"])) { attachment = new Attachment() { ContentLength = httpRequestBase.ContentLength, ContentType = httpRequestBase.ContentType, DateAdded = DateTime.Now, FileName = httpRequestBase["qqfile"], Contents = new byte[httpRequestBase.ContentLength] }; httpRequestBase.InputStream.Read(attachment.Contents, 0, httpRequestBase.ContentLength); } else { HttpPostedFileBase @base = controllerContext.RequestContext.RouteData.Values.ContainsValue("AsyncUpload") ? httpRequestBase.Files[0] : httpRequestBase.Files[bindingContext.ModelName]; var converter = new FileConverter(); attachment = converter.Convert( new ResolutionContext( new TypeMap(new TypeInfo(typeof(HttpPostedFileWrapper)), new TypeInfo(typeof(Attachment)),MemberList.Destination), @base, typeof(HttpPostedFileWrapper), typeof(Attachment),null)); } Guid attachmentId; Guid.TryParse(httpRequestBase.Form[bindingContext.ModelName + "Id"], out attachmentId); if (attachmentId == Guid.Empty && attachment == null) { return null; } if (attachmentId != Guid.Empty && attachment.IsNull()) { return new Attachment { AttachmentId = attachmentId }; } if (attachmentId != Guid.Empty && attachment.IsNotNull()) { attachment.AttachmentId = attachmentId; return attachment; } if (attachment.AttachmentId == Guid.Empty && attachment.ContentLength > 0) { return attachment; } return null; }
public FileConverterService2() { log4net.Config.XmlConfigurator.Configure(); _log = LogManager.GetLogger(typeof(FileConverterService2)); this.ServiceName = AscFileConverterServiceName; this.EventLog.Log = "Application"; fileConverter = new FileConverter(System.Environment.MachineName); this.CanHandlePowerEvent = false; this.CanHandleSessionChangeEvent = false; this.CanPauseAndContinue = false; this.CanShutdown = true; this.CanStop = true; }
private static void DownloadFile(HttpContext context, bool inline) { if (!string.IsNullOrEmpty(context.Request[CommonLinkUtility.TryParam])) { DownloadTry(context); return; } try { var id = context.Request[CommonLinkUtility.FileId]; var shareLinkKey = context.Request[CommonLinkUtility.DocShareKey] ?? ""; using (var fileDao = Global.DaoFactory.GetFileDao()) { File file; var checkLink = FileShareLink.Check(shareLinkKey, true, fileDao, out file); if (!checkLink && file == null) { int version; file = int.TryParse(context.Request[CommonLinkUtility.Version], out version) && version > 0 ? fileDao.GetFile(id, version) : fileDao.GetFile(id); } if (file == null) { context.Response.Redirect("~/404.htm"); return; } if (!checkLink && !Global.GetFilesSecurity().CanRead(file)) { context.Response.Redirect((context.Request.UrlReferrer != null ? context.Request.UrlReferrer.ToString() : PathProvider.StartURL) + "#" + UrlConstant.Error + "/" + HttpUtility.UrlEncode(FilesCommonResource.ErrorMassage_SecurityException_ReadFile)); return; } if (!fileDao.IsExistOnStorage(file)) { Global.Logger.ErrorFormat("Download file error. File is not exist on storage. File id: {0}.", file.ID); context.Response.Redirect("~/404.htm"); return; } FileMarker.RemoveMarkAsNew(file); context.Response.Clear(); context.Response.ContentType = MimeMapping.GetMimeMapping(file.Title); context.Response.Charset = "utf-8"; var browser = context.Request.Browser.Browser; var title = file.Title.Replace(',', '_'); var ext = FileUtility.GetFileExtension(file.Title); var outType = string.Empty; var curQuota = TenantExtra.GetTenantQuota(); if (curQuota.DocsEdition || FileUtility.InternalExtension.Values.Contains(ext)) { outType = context.Request[CommonLinkUtility.OutType]; } if (!string.IsNullOrEmpty(outType) && !inline) { outType = outType.Trim(); if (FileUtility.ExtsConvertible[ext].Contains(outType)) { ext = outType; title = FileUtility.ReplaceFileExtension(title, ext); } } context.Response.AddHeader("Content-Disposition", ContentDispositionUtil.GetHeaderValue(title, inline)); if (inline && string.Equals(context.Request.Headers["If-None-Match"], GetEtag(file))) { //Its cached. Reply 304 context.Response.StatusCode = (int)HttpStatusCode.NotModified; context.Response.Cache.SetETag(GetEtag(file)); } else { context.Response.CacheControl = "public"; context.Response.Cache.SetETag(GetEtag(file)); context.Response.Cache.SetCacheability(HttpCacheability.Public); Stream fileStream = null; try { if (file.ContentLength <= SetupInfo.AvailableFileSize) { if (file.ConvertedType == null && (string.IsNullOrEmpty(outType) || inline)) { context.Response.AddHeader("Content-Length", file.ContentLength.ToString(CultureInfo.InvariantCulture)); if (fileDao.IsSupportedPreSignedUri(file)) { context.Response.Redirect(fileDao.GetPreSignedUri(file, TimeSpan.FromHours(1)).ToString(), true); return; } fileStream = fileDao.GetFileStream(file); } else { fileStream = FileConverter.Exec(file, ext); } fileStream.StreamCopyTo(context.Response.OutputStream); if (!context.Response.IsClientConnected) { Global.Logger.Error(String.Format("Download file error {0} {1} Connection is lost. Too long to buffer the file", file.Title, file.ID)); } context.Response.Flush(); } else { long offset = 0; if (context.Request.Headers["Range"] != null) { context.Response.StatusCode = 206; var range = context.Request.Headers["Range"].Split(new[] { '=', '-' }); offset = Convert.ToInt64(range[1]); } if (offset > 0) { Global.Logger.Info("Starting file download offset is " + offset); } context.Response.AddHeader("Connection", "Keep-Alive"); context.Response.AddHeader("Accept-Ranges", "bytes"); if (offset > 0) { context.Response.AddHeader("Content-Range", String.Format(" bytes {0}-{1}/{2}", offset, file.ContentLength - 1, file.ContentLength)); } var dataToRead = file.ContentLength; const int bufferSize = 1024; var buffer = new Byte[bufferSize]; if (file.ConvertedType == null && (string.IsNullOrEmpty(outType) || inline)) { if (fileDao.IsSupportedPreSignedUri(file)) { context.Response.Redirect(fileDao.GetPreSignedUri(file, TimeSpan.FromHours(1)).ToString(), true); return; } fileStream = fileDao.GetFileStream(file, offset); context.Response.AddHeader("Content-Length", (file.ContentLength - offset).ToString(CultureInfo.InvariantCulture)); } else { fileStream = FileConverter.Exec(file, ext); if (offset > 0) { var startBytes = offset; while (startBytes > 0) { long readCount; if (bufferSize >= startBytes) { readCount = startBytes; } else { readCount = bufferSize; } var length = fileStream.Read(buffer, 0, (int)readCount); startBytes -= length; } } } while (dataToRead > 0) { int length; try { length = fileStream.Read(buffer, 0, bufferSize); } catch (HttpException exception) { Global.Logger.Error( String.Format("Read from stream is error. Download file {0} {1}. Maybe Connection is lost.?? Error is {2} ", file.Title, file.ID, exception )); throw; } if (context.Response.IsClientConnected) { context.Response.OutputStream.Write(buffer, 0, length); dataToRead = dataToRead - length; } else { dataToRead = -1; Global.Logger.Error(String.Format("IsClientConnected is false. Why? Download file {0} {1} Connection is lost. ", file.Title, file.ID)); } } } } catch (HttpException e) { throw new HttpException((int)HttpStatusCode.BadRequest, e.Message); } finally { if (fileStream != null) { fileStream.Flush(); fileStream.Close(); fileStream.Dispose(); } } try { context.Response.End(); } catch (HttpException) { } } } } catch (Exception ex) { // Get stack trace for the exception with source file information var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(0); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); Global.Logger.ErrorFormat("Url: {0} {1} IsClientConnected:{2}, line number:{3} frame:{4}", context.Request.Url, ex, context.Response.IsClientConnected, line, frame); context.Response.StatusCode = 400; context.Response.Write(HttpUtility.HtmlEncode(ex.Message)); } }
public void Deconstruct(out GlobalStore globalStore, out FilesLinkUtility filesLinkUtility, out SetupInfo setupInfo, out FileConverter fileConverter, out FilesMessageService filesMessageService) { globalStore = GlobalStore; filesLinkUtility = FilesLinkUtility; setupInfo = SetupInfo; fileConverter = FileConverter; filesMessageService = FilesMessageService; }
//ctor public ControllerWithAsync(IDatabaseService <T> dbService, FileConverter fileConverter, ISorter <T> idiomSorter) { this.dbService = dbService; this.fileConverter = fileConverter; this.sorter = idiomSorter; }
public async Task <JsonResult> ApplyNow() { try { var appUser = await userManager.GetUserAsync(HttpContext.User); var document = dbContext.GetDocument(appUser, "documentName"); var documentEmail = (DocumentEmail)dbContext.GetDbObject("DocumentEmail", appUser, document); var userValues = (UserValues)dbContext.GetDbObject("UserValues", appUser, document); var customVariables = (IEnumerable <CustomVariable>)dbContext.GetDbObject("CustomVariables", appUser, document); var documentFiles = (IEnumerable <DocumentFile>)dbContext.GetDbObject("DocumentFiles", appUser, document); var sentApplication = new SentApplication { Document = document, UserValues = userValues, SentDate = DateTime.Now }; dbContext.Add(sentApplication); var pdfFilePaths = new List <string>(); var dict = getVariableDict(document.Employer, userValues, documentEmail, customVariables, document.JobName); var tmpDirectory = new TmpPath().Path; var userDirectory = new UserPath(appUser.Id).UserDirectory; foreach (var documentFile in documentFiles) { var currentFile = Path.Combine(userDirectory, documentFile.Path); var extension = Path.GetExtension(currentFile).ToLower(); var convertedToPdfPath = Path.Combine(tmpDirectory, $"{Path.GetFileNameWithoutExtension(documentFile.Path)}_{Guid.NewGuid().ToString()}.pdf"); if (extension == ".odt") { var tmpPath2 = Path.Combine(tmpDirectory, $"{Path.GetFileNameWithoutExtension(documentFile.Path)}_replaced_{Guid.NewGuid().ToString()}{extension}"); FileConverter.ReplaceInOdt(currentFile, tmpPath2, dict); currentFile = tmpPath2; } if (FileConverter.ConvertTo(currentFile, convertedToPdfPath)) { pdfFilePaths.Add(convertedToPdfPath); } else { throw new Exception("Failed to convert file " + documentFile.Name); } } var mergedPath = Path.Combine(new TmpPath().Path, "merged_" + Guid.NewGuid().ToString() + ".pdf"); if (!FileConverter.MergePdfs(pdfFilePaths, mergedPath)) { throw new Exception("Failed to merge pdfs."); } var newEmployer = new Employer(); dbContext.Add(newEmployer); var documentCopy = new Document(document) { Employer = newEmployer }; dbContext.Add(documentCopy); dbContext.Add(new UserValues(userValues) { AppUser = appUser }); dbContext.Add(new DocumentEmail(documentEmail) { Document = documentCopy }); foreach (var customVariable in customVariables) { dbContext.Add(new CustomVariable(customVariable) { Document = documentCopy }); } foreach (var documentFile in documentFiles) { dbContext.Add(new DocumentFile(documentFile) { Document = documentCopy }); } var attachments = new List <EmailAttachment>(); if (pdfFilePaths.Count >= 1) { var attachmentName = FileConverter.ReplaceInString(documentEmail.AttachmentName, dict); if (attachmentName.Length >= 1 && !attachmentName.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase)) { attachmentName = attachmentName + ".pdf"; } else if (!(attachmentName.Length >= 4 && attachmentName.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase))) { attachmentName = "Bewerbung.pdf"; } attachments.Add(new EmailAttachment { Path = mergedPath, Name = attachmentName }); } HelperFunctions.SendEmail( new EmailData { Attachments = attachments, Body = FileConverter.ReplaceInString(documentEmail.Body, dict), Subject = FileConverter.ReplaceInString(documentEmail.Subject, dict), ToEmail = document.Employer.Email, FromEmail = userValues.Email, FromName = (userValues.Degree == "" ? "" : userValues.Degree + " ") + userValues.FirstName + " " + userValues.LastName } ); dbContext.SaveChanges(); return(Json(new { status = 0 })); } catch (Exception err) { log.Error("", err); return(Json(new { status = 1 })); } }
private Stream CompressToZip(ItemNameValueCollection entriesPathId) { var stream = TempStream.Create(); using (var zip = new ionic::Ionic.Zip.ZipOutputStream(stream, true)) { zip.CompressionLevel = ionic::Ionic.Zlib.CompressionLevel.Level3; zip.AlternateEncodingUsage = ionic::Ionic.Zip.ZipOption.AsNecessary; zip.AlternateEncoding = Encoding.UTF8; foreach (var path in entriesPathId.AllKeys) { var counter = 0; foreach (var entryId in entriesPathId[path]) { if (CancellationToken.IsCancellationRequested) { zip.Dispose(); stream.Dispose(); CancellationToken.ThrowIfCancellationRequested(); } var newtitle = path; File file = null; var convertToExt = string.Empty; if (!string.IsNullOrEmpty(entryId)) { FileDao.InvalidateCache(entryId); file = FileDao.GetFile(entryId); if (file == null) { Error = FilesCommonResource.ErrorMassage_FileNotFound; continue; } if (file.ContentLength > SetupInfo.AvailableFileSize) { Error = string.Format(FilesCommonResource.ErrorMassage_FileSizeZip, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize)); continue; } if (files.ContainsKey(file.ID.ToString())) { convertToExt = files[file.ID.ToString()]; if (!string.IsNullOrEmpty(convertToExt)) { newtitle = FileUtility.ReplaceFileExtension(path, convertToExt); } } } if (0 < counter) { var suffix = " (" + counter + ")"; if (!string.IsNullOrEmpty(entryId)) { newtitle = 0 < newtitle.IndexOf('.') ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix) : newtitle + suffix; } else { break; } } zip.PutNextEntry(newtitle); if (!string.IsNullOrEmpty(entryId) && file != null) { try { if (FileConverter.EnableConvert(file, convertToExt)) { //Take from converter using (var readStream = FileConverter.Exec(file, convertToExt)) { readStream.StreamCopyTo(zip); if (!string.IsNullOrEmpty(convertToExt)) { FilesMessageService.Send(file, headers, MessageAction.FileDownloadedAs, file.Title, convertToExt); } else { FilesMessageService.Send(file, headers, MessageAction.FileDownloaded, file.Title); } } } else { using (var readStream = FileDao.GetFileStream(file)) { readStream.StreamCopyTo(zip); FilesMessageService.Send(file, headers, MessageAction.FileDownloaded, file.Title); } } } catch (Exception ex) { Error = ex.Message; Logger.Error(Error, ex); } } counter++; } ProgressStep(); } } return(stream); }
public QuizzesController(IDatabaseService <Quiz> dbService, FileConverter fileConverter, ISorter <Quiz> idiomSorter, IDatabaseService <Idiom> idiomsDbService) : base(dbService, fileConverter, idiomSorter) { this.idiomsDbService = idiomsDbService; }
private static void DownloadFile(HttpContext context) { var flushed = false; try { var id = context.Request[FilesLinkUtility.FileId]; var doc = context.Request[FilesLinkUtility.DocShareKey] ?? ""; using (var fileDao = Global.DaoFactory.GetFileDao()) { File file; var readLink = FileShareLink.Check(doc, true, fileDao, out file); if (!readLink && file == null) { fileDao.InvalidateCache(id); int version; file = int.TryParse(context.Request[FilesLinkUtility.Version], out version) && version > 0 ? fileDao.GetFile(id, version) : fileDao.GetFile(id); } if (file == null) { context.Response.StatusCode = (int)HttpStatusCode.NotFound; return; } if (!readLink && !Global.GetFilesSecurity().CanRead(file)) { context.Response.StatusCode = (int)HttpStatusCode.Forbidden; return; } if (!string.IsNullOrEmpty(file.Error)) { throw new Exception(file.Error); } if (!fileDao.IsExistOnStorage(file)) { Global.Logger.ErrorFormat("Download file error. File is not exist on storage. File id: {0}.", file.ID); context.Response.StatusCode = (int)HttpStatusCode.NotFound; return; } FileMarker.RemoveMarkAsNew(file); context.Response.Clear(); context.Response.ClearHeaders(); context.Response.Charset = "utf-8"; var title = file.Title.Replace(',', '_'); var ext = FileUtility.GetFileExtension(file.Title); var outType = context.Request[FilesLinkUtility.OutType]; if (!string.IsNullOrEmpty(outType)) { outType = outType.Trim(); if (FileUtility.ExtsConvertible[ext].Contains(outType)) { ext = outType; title = FileUtility.ReplaceFileExtension(title, ext); } } context.Response.AddHeader("Content-Disposition", ContentDispositionUtil.GetHeaderValue(title)); context.Response.ContentType = MimeMapping.GetMimeMapping(title); //// Download file via nginx //if (CoreContext.Configuration.Standalone && // WorkContext.IsMono && // Global.GetStore() is DiscDataStore && // !file.ProviderEntry && // !FileConverter.EnableConvert(file, ext) // ) //{ // var diskDataStore = (DiscDataStore)Global.GetStore(); // var pathToFile = diskDataStore.GetPhysicalPath(String.Empty, FileDao.GetUniqFilePath(file)); // context.Response.Headers.Add("X-Accel-Redirect", "/filesData" + pathToFile); // FilesMessageService.Send(file, context.Request, MessageAction.FileDownloaded, file.Title); // return; //} if (string.Equals(context.Request.Headers["If-None-Match"], GetEtag(file))) { //Its cached. Reply 304 context.Response.StatusCode = (int)HttpStatusCode.NotModified; context.Response.Cache.SetETag(GetEtag(file)); } else { context.Response.CacheControl = "public"; context.Response.Cache.SetETag(GetEtag(file)); context.Response.Cache.SetCacheability(HttpCacheability.Public); Stream fileStream = null; try { if (file.ContentLength <= SetupInfo.AvailableFileSize) { if (!FileConverter.EnableConvert(file, ext)) { if (!readLink && fileDao.IsSupportedPreSignedUri(file)) { context.Response.Redirect(fileDao.GetPreSignedUri(file, TimeSpan.FromHours(1)).ToString(), true); return; } fileStream = fileDao.GetFileStream(file); context.Response.AddHeader("Content-Length", file.ContentLength.ToString(CultureInfo.InvariantCulture)); } else { fileStream = FileConverter.Exec(file, ext); context.Response.AddHeader("Content-Length", fileStream.Length.ToString(CultureInfo.InvariantCulture)); } fileStream.StreamCopyTo(context.Response.OutputStream); if (!context.Response.IsClientConnected) { Global.Logger.Warn(String.Format("Download file error {0} {1} Connection is lost. Too long to buffer the file", file.Title, file.ID)); } FilesMessageService.Send(file, context.Request, MessageAction.FileDownloaded, file.Title); context.Response.Flush(); flushed = true; } else { context.Response.Buffer = false; context.Response.ContentType = "application/octet-stream"; long offset = 0; if (context.Request.Headers["Range"] != null) { context.Response.StatusCode = 206; var range = context.Request.Headers["Range"].Split(new[] { '=', '-' }); offset = Convert.ToInt64(range[1]); } if (offset > 0) { Global.Logger.Info("Starting file download offset is " + offset); } context.Response.AddHeader("Connection", "Keep-Alive"); context.Response.AddHeader("Accept-Ranges", "bytes"); if (offset > 0) { context.Response.AddHeader("Content-Range", String.Format(" bytes {0}-{1}/{2}", offset, file.ContentLength - 1, file.ContentLength)); } var dataToRead = file.ContentLength; const int bufferSize = 8 * 1024; // 8KB var buffer = new Byte[bufferSize]; if (!FileConverter.EnableConvert(file, ext)) { if (!readLink && fileDao.IsSupportedPreSignedUri(file)) { context.Response.Redirect(fileDao.GetPreSignedUri(file, TimeSpan.FromHours(1)).ToString(), true); return; } fileStream = fileDao.GetFileStream(file, offset); context.Response.AddHeader("Content-Length", (file.ContentLength - offset).ToString(CultureInfo.InvariantCulture)); } else { fileStream = FileConverter.Exec(file, ext); if (offset > 0) { var startBytes = offset; while (startBytes > 0) { long readCount; if (bufferSize >= startBytes) { readCount = startBytes; } else { readCount = bufferSize; } var length = fileStream.Read(buffer, 0, (int)readCount); startBytes -= length; } } } while (dataToRead > 0) { int length; try { length = fileStream.Read(buffer, 0, bufferSize); } catch (HttpException exception) { Global.Logger.Error( String.Format("Read from stream is error. Download file {0} {1}. Maybe Connection is lost.?? Error is {2} ", file.Title, file.ID, exception )); throw; } if (context.Response.IsClientConnected) { context.Response.OutputStream.Write(buffer, 0, length); context.Response.Flush(); flushed = true; dataToRead = dataToRead - length; } else { dataToRead = -1; Global.Logger.Warn(String.Format("IsClientConnected is false. Why? Download file {0} {1} Connection is lost. ", file.Title, file.ID)); } } } } catch (ThreadAbortException) { } catch (HttpException e) { throw new HttpException((int)HttpStatusCode.BadRequest, e.Message); } finally { if (fileStream != null) { fileStream.Close(); fileStream.Dispose(); } } try { context.Response.End(); flushed = true; } catch (HttpException) { } } } } catch (ThreadAbortException) { } catch (Exception ex) { // Get stack trace for the exception with source file information var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(0); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); Global.Logger.ErrorFormat("Url: {0} {1} IsClientConnected:{2}, line number:{3} frame:{4}", context.Request.Url, ex, context.Response.IsClientConnected, line, frame); if (!flushed && context.Response.IsClientConnected) { context.Response.StatusCode = 400; context.Response.Write(HttpUtility.HtmlEncode(ex.Message)); } } }
private Stream CompressTo(ItemNameValueCollection entriesPathId) { var stream = TempStream.Create(); using (ICompress compressTo = new CompressToArchive(stream)) { foreach (var path in entriesPathId.AllKeys) { var counter = 0; foreach (var entryId in entriesPathId[path]) { if (CancellationToken.IsCancellationRequested) { compressTo.Dispose(); stream.Dispose(); CancellationToken.ThrowIfCancellationRequested(); } var newtitle = path; File file = null; var convertToExt = string.Empty; if (!string.IsNullOrEmpty(entryId)) { FileDao.InvalidateCache(entryId); file = FileDao.GetFile(entryId); if (file == null) { Error = FilesCommonResource.ErrorMassage_FileNotFound; continue; } if (files.ContainsKey(file.ID.ToString())) { convertToExt = files[file.ID.ToString()]; if (!string.IsNullOrEmpty(convertToExt)) { newtitle = FileUtility.ReplaceFileExtension(path, convertToExt); } } } if (0 < counter) { var suffix = " (" + counter + ")"; if (!string.IsNullOrEmpty(entryId)) { newtitle = 0 < newtitle.IndexOf('.') ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix) : newtitle + suffix; } else { break; } } compressTo.CreateEntry(newtitle); if (!string.IsNullOrEmpty(entryId) && file != null) { try { if (FileConverter.EnableConvert(file, convertToExt)) { //Take from converter using (var readStream = FileConverter.Exec(file, convertToExt)) { compressTo.PutStream(readStream); if (!string.IsNullOrEmpty(convertToExt)) { FilesMessageService.Send(file, headers, MessageAction.FileDownloadedAs, file.Title, convertToExt); } else { FilesMessageService.Send(file, headers, MessageAction.FileDownloaded, file.Title); } } } else { using (var readStream = FileDao.GetFileStream(file)) { compressTo.PutStream(readStream); FilesMessageService.Send(file, headers, MessageAction.FileDownloaded, file.Title); } } } catch (Exception ex) { Error = ex.Message; Logger.Error(Error, ex); } } else { compressTo.PutNextEntry(); } compressTo.CloseEntry(); counter++; } ProgressStep(); } } return(stream); }
private void PageLoad() { var editPossible = !RequestEmbedded; var isExtenral = false; File file; var fileUri = string.Empty; IThirdPartyApp app = null; try { if (string.IsNullOrEmpty(RequestFileUrl)) { _fileNew = (Request["new"] ?? "") == "true"; app = ThirdPartySelector.GetAppByFileId(RequestFileId); if (app == null) { var ver = string.IsNullOrEmpty(Request[FilesLinkUtility.Version]) ? -1 : Convert.ToInt32(Request[FilesLinkUtility.Version]); file = DocumentServiceHelper.GetParams(RequestFileId, ver, RequestShareLinkKey, _fileNew, editPossible, !RequestView, out _docParams); _fileNew = _fileNew && file.Version == 1 && file.ConvertedType != null && file.CreateOn == file.ModifiedOn; } else { isExtenral = true; bool editable; _thirdPartyApp = true; file = app.GetFile(RequestFileId, out editable); file = DocumentServiceHelper.GetParams(file, true, true, true, editable, editable, editable, out _docParams); _docParams.FileUri = app.GetFileStreamUrl(file); _docParams.FolderUrl = string.Empty; } } else { isExtenral = true; fileUri = RequestFileUrl; var fileTitle = Request[FilesLinkUtility.FileTitle]; if (string.IsNullOrEmpty(fileTitle)) { fileTitle = Path.GetFileName(HttpUtility.UrlDecode(fileUri)) ?? ""; } if (CoreContext.Configuration.Standalone) { try { var webRequest = WebRequest.Create(RequestFileUrl); using (var response = webRequest.GetResponse()) using (var responseStream = new ResponseStream(response)) { var externalFileKey = DocumentServiceConnector.GenerateRevisionId(RequestFileUrl); fileUri = DocumentServiceConnector.GetExternalUri(responseStream, MimeMapping.GetMimeMapping(fileTitle), externalFileKey); } } catch (Exception error) { Global.Logger.Error("Cannot receive external url for \"" + RequestFileUrl + "\"", error); } } file = new File { ID = RequestFileUrl, Title = Global.ReplaceInvalidCharsAndTruncate(fileTitle) }; file = DocumentServiceHelper.GetParams(file, true, true, true, false, false, false, out _docParams); _docParams.CanEdit = editPossible && !CoreContext.Configuration.Standalone; _editByUrl = true; _docParams.FileUri = fileUri; } } catch (Exception ex) { _errorMessage = ex.Message; return; } if (_docParams.ModeWrite && FileConverter.MustConvert(file)) { try { file = FileConverter.ExecDuplicate(file, RequestShareLinkKey); } catch (Exception e) { _docParams = null; _errorMessage = e.Message; return; } var comment = "#message/" + HttpUtility.UrlEncode(FilesCommonResource.CopyForEdit); Response.Redirect(FilesLinkUtility.GetFileWebEditorUrl(file.ID) + comment); return; } Title = file.Title; _newScheme = FileUtility.ExtsNewService.Contains(FileUtility.GetFileExtension(file.Title)) && (string.IsNullOrEmpty(RequestShareLinkKey) ? OnlineEditorsSettings.NewScheme : OnlineEditorsSettings.NewSchemeFor(file.CreateBy)) && app == null; if (_newScheme) { DocServiceApiUrl = FilesLinkUtility.DocServiceApiUrlNew; } if (string.IsNullOrEmpty(_docParams.FolderUrl)) { _docParams.FolderUrl = Request[FilesLinkUtility.FolderUrl] ?? ""; } if (MobileDetector.IsRequestMatchesMobile(true)) { _docParams.FolderUrl = string.Empty; } if (RequestEmbedded) { _docParams.Type = DocumentServiceParams.EditorType.Embedded; var shareLinkParam = "&" + FilesLinkUtility.DocShareKey + "=" + RequestShareLinkKey; _docParams.ViewerUrl = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.FilesBaseAbsolutePath + FilesLinkUtility.EditorPage + "?" + FilesLinkUtility.Action + "=view" + shareLinkParam); _docParams.DownloadUrl = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.FileHandlerPath + "?" + FilesLinkUtility.Action + "=download" + shareLinkParam); _docParams.EmbeddedUrl = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.FilesBaseAbsolutePath + FilesLinkUtility.EditorPage + "?" + FilesLinkUtility.Action + "=embedded" + shareLinkParam); } else { _docParams.Type = IsMobile ? DocumentServiceParams.EditorType.Mobile : DocumentServiceParams.EditorType.Desktop; if (FileSharing.CanSetAccess(file)) { _docParams.SharingSettingsUrl = CommonLinkUtility.GetFullAbsolutePath(Share.Location + "?" + FilesLinkUtility.FileId + "=" + file.ID); } } if (!isExtenral) { _docKeyForTrack = DocumentServiceHelper.GetDocKey(file.ID, -1, DateTime.MinValue); FileMarker.RemoveMarkAsNew(file); } if (_docParams.ModeWrite) { _tabId = FileTracker.Add(file.ID, _fileNew); _fixedVersion = FileTracker.FixedVersion(file.ID); } else { _docParams.LinkToEdit = _editByUrl ? CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorExternalUrl(fileUri, file.Title)) : FileConverter.MustConvert(_docParams.File) || _newScheme ? CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorUrl(file.ID)) : string.Empty; } }
static void Init() { FileConverter window = (FileConverter)EditorWindow.GetWindow(typeof(FileConverter)); window.Show(); }
// called to extract text from a file // passed - sourceFileName - full path of file to extract text from // returns - // indexText - text that will be used for Lucene indexing, includes metadata // analysisText - text that will be used for clustering and LSA // errorFlag - false if an unrecoverable error occurred, else true // errorText - text of error if one occurred public void extractText(string docID, string sourceFileName, string title, ref string indexText, ref string analysisText, List<ErrorDataObject> errObjs, out bool errorFlag) { Options dtOptions; FileConverter fileConverter; StringBuilder outStringIndex = new StringBuilder(); StringBuilder outStringAnalysis = new StringBuilder(); errorFlag = true; indexText = ""; analysisText = ""; try { // construct temporary file name for xml file output by dtSearch string targetFileNameDTSearch = @"C:\temp\_DTSearch.txt"; File.Delete(targetFileNameDTSearch); dtOptions = new Options(); dtOptions.FieldFlags = FieldFlags.dtsoFfOfficeSkipHiddenContent; dtOptions.BinaryFiles = BinaryFilesSettings.dtsoIndexSkipBinary; dtOptions.Save(); fileConverter = new FileConverter(); fileConverter.InputFile = sourceFileName; fileConverter.OutputFile = targetFileNameDTSearch; fileConverter.OutputFormat = OutputFormats.it_ContentAsXml; fileConverter.Flags = ConvertFlags.dtsConvertInlineContainer; fileConverter.Execute(); //check for image file type TypeId deType = fileConverter.DetectedTypeId; if (imageTypes.Contains(deType)) { errObjs.Add(new ErrorDataObject("1002", "Image File Type: " + deType.ToString(), "Warning")); } // return if there is a dtSearch error other than file corrupt (10) or file encrypted (17) JobErrorInfo errorInfo = fileConverter.Errors; bool fatalError = false; bool fileMissingOrNoText = false; int dtErCode = 0; if (errorInfo != null && errorInfo.Count > 0) { for (int i = 0; i < errorInfo.Count; i++) { dtErCode = errorInfo.Code(i); string errorCode = ""; if (dtErCode != 9 && dtErCode != 10 && dtErCode != 17 && dtErCode != 207 && dtErCode != 16 && dtErCode != 21) { errObjs.Add(new ErrorDataObject("1005", "Text extraction Error occurred during processing of the document. " + errorInfo.Message(i), "Error")); fatalError = true; } else { string errText = ""; if (dtErCode == 10) // dtsErFileCorrupt { errorCode = "1013"; errText = "Document is corrupted."; } if (dtErCode == 17) // dtsErFileEncrypted { errorCode = "1007"; errText = "A component of the document is encrypted."; } if (dtErCode == 207) // dtsErContainerItemEncrypted, internal error code { errorCode = "1014"; errText = "The document is encrypted."; string text = errorInfo.Message(i); if (text != null) { int index = text.IndexOf("->"); if (index >= 0) { errText = "A component of the document is encrypted. " + text.Substring(index); } } } if (dtErCode == 9) // dtsErAccFile { errorCode = "1010"; errText = "The system cannot access the file specified."; } if (dtErCode == 16) // dtsErFileNotFound { errorCode = "1011"; errText = "Document file does not exist."; } if (dtErCode == 21) // dtsErFileEmpty { errorCode = "1012"; errText = "Document file is empty"; } if (dtErCode == 9 || dtErCode == 10 || dtErCode == 207 || dtErCode == 16 || dtErCode == 21) fileMissingOrNoText = true; // file missing, no text, corrupt or encrypted if (errText == "") errText = errorInfo.Message(i); errObjs.Add(new ErrorDataObject(errorCode, "Text extraction error: " + errText, "Warning")); } } } if (fatalError) { errorFlag = false; return; } else { if (fileMissingOrNoText) return; if (dtErCode == 17) { FileInfo fi = new FileInfo(targetFileNameDTSearch); if (fi.Length == 0) { errObjs.Clear(); // remove error "1007" errObjs.Add(new ErrorDataObject("1014", "Text extraction error: document is encrypted.", "Warning")); return; } } //load the dtSearch XML output file into an XML document XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(targetFileNameDTSearch); } catch { //try cleaning up the metadata tags and loading again cleanMetadataTags(targetFileNameDTSearch); xmlDoc.Load(targetFileNameDTSearch); } //start with the document node XmlNode docNode = xmlDoc.DocumentElement; //initialize the output strings outStringIndex.Length = 0; outStringIndex.AppendLine("DocID: " + docID); if (title != null && title.Length > 0) outStringIndex.AppendLine("Filename: " + title); outStringAnalysis.Length = 0; //start outputting with the document node outputNode(docNode, outStringIndex, outStringAnalysis, errObjs); indexText = outStringIndex.ToString(); analysisText = outStringAnalysis.ToString(); //signal error if no analysis text if (analysisText.Length == 0) errObjs.Add(new ErrorDataObject("1003", "No text to analyze", "Warning")); return; } } catch (Exception ex) { errObjs.Add(new ErrorDataObject("1001", "Text extraction Error occurred during processing of the document. " + ex.Message, "Error")); errorFlag = false; return; } }
public FileDownloadOperationScope(GlobalStore globalStore, FilesLinkUtility filesLinkUtility, SetupInfo setupInfo, FileConverter fileConverter, FilesMessageService filesMessageService) { GlobalStore = globalStore; FilesLinkUtility = filesLinkUtility; SetupInfo = setupInfo; FileConverter = fileConverter; FilesMessageService = filesMessageService; }
private Stream CompressToZip(ItemNameValueCollection entriesPathId) { var stream = TempStream.Create(); using (var zip = new ZipOutputStream(stream, true)) { zip.CompressionLevel = CompressionLevel.Level3; zip.AlternateEncodingUsage = ZipOption.AsNecessary; zip.AlternateEncoding = Encoding.GetEncoding(Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage); foreach (var path in entriesPathId.AllKeys) { if (Canceled) { zip.Dispose(); stream.Dispose(); return(null); } var counter = 0; foreach (var entryId in entriesPathId[path]) { var newtitle = path; File file = null; var convertToExt = string.Empty; if (!string.IsNullOrEmpty(entryId)) { file = FileDao.GetFile(entryId); if (file.ContentLength > SetupInfo.AvailableFileSize) { Error = string.Format(FilesCommonResource.ErrorMassage_FileSizeZip, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize)); continue; } if (_files.ContainsKey(file.ID.ToString())) { if (_quotaDocsEdition || FileUtility.InternalExtension.Values.Contains(convertToExt)) { convertToExt = _files[file.ID.ToString()]; } if (!string.IsNullOrEmpty(convertToExt)) { newtitle = FileUtility.ReplaceFileExtension(path, convertToExt); } } } if (0 < counter) { var suffix = " (" + counter + ")"; if (!string.IsNullOrEmpty(entryId)) { newtitle = 0 < newtitle.IndexOf('.') ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix) : newtitle + suffix; } else { break; } } zip.PutNextEntry(newtitle); if (!string.IsNullOrEmpty(entryId) && file != null) { if (file.ConvertedType != null || !string.IsNullOrEmpty(convertToExt)) { //Take from converter try { using (var readStream = !string.IsNullOrEmpty(convertToExt) ? FileConverter.Exec(file, convertToExt) : FileConverter.Exec(file)) { if (readStream != null) { readStream.StreamCopyTo(zip); } } } catch (Exception ex) { Error = ex.Message; Logger.Error(Error, ex); } } else { using (var readStream = FileDao.GetFileStream(file)) { readStream.StreamCopyTo(zip); } } } counter++; } ProgressStep(); } return(stream); } }
private static void DownloadFile(HttpContext context) { var flushed = false; try { var id = context.Request[FilesLinkUtility.FileId]; var doc = context.Request[FilesLinkUtility.DocShareKey] ?? ""; using (var fileDao = Global.DaoFactory.GetFileDao()) { File file; var readLink = FileShareLink.Check(doc, true, fileDao, out file); if (!readLink && file == null) { fileDao.InvalidateCache(id); int version; file = int.TryParse(context.Request[FilesLinkUtility.Version], out version) && version > 0 ? fileDao.GetFile(id, version) : fileDao.GetFile(id); } if (file == null) { context.Response.StatusCode = (int)HttpStatusCode.NotFound; return; } if (!readLink && !Global.GetFilesSecurity().CanRead(file)) { context.Response.StatusCode = (int)HttpStatusCode.Forbidden; return; } if (!string.IsNullOrEmpty(file.Error)) { throw new Exception(file.Error); } if (!fileDao.IsExistOnStorage(file)) { Global.Logger.ErrorFormat("Download file error. File is not exist on storage. File id: {0}.", file.ID); context.Response.StatusCode = (int)HttpStatusCode.NotFound; return; } FileMarker.RemoveMarkAsNew(file); context.Response.Clear(); context.Response.ClearHeaders(); context.Response.Charset = "utf-8"; FilesMessageService.Send(file, context.Request, MessageAction.FileDownloaded, file.Title); if (string.Equals(context.Request.Headers["If-None-Match"], GetEtag(file))) { //Its cached. Reply 304 context.Response.StatusCode = (int)HttpStatusCode.NotModified; context.Response.Cache.SetETag(GetEtag(file)); } else { context.Response.CacheControl = "public"; context.Response.Cache.SetETag(GetEtag(file)); context.Response.Cache.SetCacheability(HttpCacheability.Public); Stream fileStream = null; try { var title = file.Title; if (file.ContentLength <= SetupInfo.AvailableFileSize) { var ext = FileUtility.GetFileExtension(file.Title); var outType = (context.Request[FilesLinkUtility.OutType] ?? "").Trim(); if (!string.IsNullOrEmpty(outType) && FileUtility.ExtsConvertible.Keys.Contains(ext) && FileUtility.ExtsConvertible[ext].Contains(outType)) { ext = outType; } long offset = 0; long length; if (!file.ProviderEntry && string.Equals(context.Request["convpreview"], "true", StringComparison.InvariantCultureIgnoreCase) && FFmpegService.IsConvertable(ext)) { const string mp4Name = "content.mp4"; var mp4Path = FileDao.GetUniqFilePath(file, mp4Name); var store = Global.GetStore(); if (!store.IsFile(mp4Path)) { fileStream = fileDao.GetFileStream(file); Global.Logger.InfoFormat("Converting {0} (fileId: {1}) to mp4", file.Title, file.ID); var stream = FFmpegService.Convert(fileStream, ext); store.Save(string.Empty, mp4Path, stream, mp4Name); } var fullLength = store.GetFileSize(string.Empty, mp4Path); length = ProcessRangeHeader(context, fullLength, ref offset); fileStream = store.GetReadStream(string.Empty, mp4Path, (int)offset); title = FileUtility.ReplaceFileExtension(title, ".mp4"); } else { if (!FileConverter.EnableConvert(file, ext)) { if (!readLink && fileDao.IsSupportedPreSignedUri(file)) { context.Response.Redirect(fileDao.GetPreSignedUri(file, TimeSpan.FromHours(1)).ToString(), true); return; } fileStream = fileDao.GetFileStream(file); // getStream to fix file.ContentLength if (fileStream.CanSeek) { var fullLength = file.ContentLength; length = ProcessRangeHeader(context, fullLength, ref offset); fileStream.Seek(offset, SeekOrigin.Begin); } else { length = file.ContentLength; } } else { title = FileUtility.ReplaceFileExtension(title, ext); fileStream = FileConverter.Exec(file, ext); length = fileStream.Length; } } SendStreamByChunks(context, length, title, fileStream, ref flushed); } else { if (!readLink && fileDao.IsSupportedPreSignedUri(file)) { context.Response.Redirect(fileDao.GetPreSignedUri(file, TimeSpan.FromHours(1)).ToString(), true); return; } fileStream = fileDao.GetFileStream(file); // getStream to fix file.ContentLength long offset = 0; var length = file.ContentLength; if (fileStream.CanSeek) { length = ProcessRangeHeader(context, file.ContentLength, ref offset); fileStream.Seek(offset, SeekOrigin.Begin); } SendStreamByChunks(context, length, title, fileStream, ref flushed); } } catch (ThreadAbortException tae) { Global.Logger.Error("DownloadFile", tae); } catch (HttpException e) { Global.Logger.Error("DownloadFile", e); throw new HttpException((int)HttpStatusCode.BadRequest, e.Message); } finally { if (fileStream != null) { fileStream.Close(); fileStream.Dispose(); } } try { context.Response.Flush(); context.Response.SuppressContent = true; context.ApplicationInstance.CompleteRequest(); flushed = true; } catch (HttpException ex) { Global.Logger.Error("DownloadFile", ex); } } } } catch (ThreadAbortException tae) { Global.Logger.Error("DownloadFile", tae); } catch (Exception ex) { // Get stack trace for the exception with source file information var st = new StackTrace(ex, true); // Get the top stack frame var frame = st.GetFrame(0); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); Global.Logger.ErrorFormat("Url: {0} {1} IsClientConnected:{2}, line number:{3} frame:{4}", context.Request.Url, ex, context.Response.IsClientConnected, line, frame); if (!flushed && context.Response.IsClientConnected) { context.Response.StatusCode = 400; context.Response.Write(HttpUtility.HtmlEncode(ex.Message)); } } }
public MailAttachment AttachFileFromDocuments(int tenant, string user, int messageId, string fileId, string version, string shareLink) { MailAttachment result; using (var fileDao = FilesIntegration.GetFileDao()) { File file; var checkLink = FileShareLink.Check(shareLink, true, fileDao, out file); if (!checkLink && file == null) { file = string.IsNullOrEmpty(version) ? fileDao.GetFile(fileId) : fileDao.GetFile(fileId, Convert.ToInt32(version)); } if (file == null) { throw new AttachmentsException(AttachmentsException.Types.DocumentNotFound, "File not found."); } if (!checkLink && !FilesIntegration.GetFileSecurity().CanRead(file)) { throw new AttachmentsException(AttachmentsException.Types.DocumentAccessDenied, "Access denied."); } if (!fileDao.IsExistOnStorage(file)) { throw new AttachmentsException(AttachmentsException.Types.DocumentNotFound, "File not exists on storage."); } _log.Info("Original file id: {0}", file.ID); _log.Info("Original file name: {0}", file.Title); var fileExt = FileUtility.GetFileExtension(file.Title); var curFileType = FileUtility.GetFileTypeByFileName(file.Title); _log.Info("File converted type: {0}", file.ConvertedType); if (file.ConvertedType != null) { switch (curFileType) { case FileType.Image: fileExt = file.ConvertedType == ".zip" ? ".pptt" : file.ConvertedType; break; case FileType.Spreadsheet: fileExt = file.ConvertedType != ".xlsx" ? ".xlst" : file.ConvertedType; break; default: if (file.ConvertedType == ".doct" || file.ConvertedType == ".xlst" || file.ConvertedType == ".pptt") { fileExt = file.ConvertedType; } break; } } var convertToExt = string.Empty; switch (curFileType) { case FileType.Document: if (fileExt == ".doct") { convertToExt = ".docx"; } break; case FileType.Spreadsheet: if (fileExt == ".xlst") { convertToExt = ".xlsx"; } break; case FileType.Presentation: if (fileExt == ".pptt") { convertToExt = ".pptx"; } break; } if (!string.IsNullOrEmpty(convertToExt) && fileExt != convertToExt) { var fileName = Path.ChangeExtension(file.Title, convertToExt); _log.Info("Changed file name - {0} for file {1}:", fileName, file.ID); using (var readStream = FileConverter.Exec(file, convertToExt)) { if (readStream == null) { throw new AttachmentsException(AttachmentsException.Types.DocumentAccessDenied, "Access denied."); } using (var memStream = new MemoryStream()) { readStream.StreamCopyTo(memStream); result = AttachFileToDraft(tenant, user, messageId, fileName, memStream); _log.Info("Attached attachment: ID - {0}, Name - {1}, StoredUrl - {2}", result.fileName, result.fileName, result.storedFileUrl); } } } else { using (var readStream = fileDao.GetFileStream(file)) { if (readStream == null) { throw new AttachmentsException(AttachmentsException.Types.DocumentAccessDenied, "Access denied."); } result = AttachFileToDraft(tenant, user, messageId, file.Title, readStream); _log.Info("Attached attachment: ID - {0}, Name - {1}, StoredUrl - {2}", result.fileName, result.fileName, result.storedFileUrl); } } } return(result); }
// called to extract text from a file // passed - sourceFileName - full path of file to extract text from // returns - // indexText - text that will be used for Lucene indexing, includes metadata // analysisText - text that will be used for clustering and LSA // errorFlag - false if an unrecoverable error occurred, else true // errorText - text of error if one occurred public void extractText(string docID, string sourceFileName, string title, ref string indexText, ref string analysisText, List <ErrorDataObject> errObjs, out bool errorFlag) { Options dtOptions; FileConverter fileConverter; StringBuilder outStringIndex = new StringBuilder(); StringBuilder outStringAnalysis = new StringBuilder(); errorFlag = true; indexText = ""; analysisText = ""; try { // construct temporary file name for xml file output by dtSearch string targetFileNameDTSearch = @"C:\temp\_DTSearch.txt"; File.Delete(targetFileNameDTSearch); dtOptions = new Options(); dtOptions.FieldFlags = FieldFlags.dtsoFfOfficeSkipHiddenContent; dtOptions.BinaryFiles = BinaryFilesSettings.dtsoIndexSkipBinary; dtOptions.Save(); fileConverter = new FileConverter(); fileConverter.InputFile = sourceFileName; fileConverter.OutputFile = targetFileNameDTSearch; fileConverter.OutputFormat = OutputFormats.it_ContentAsXml; fileConverter.Flags = ConvertFlags.dtsConvertInlineContainer; fileConverter.Execute(); //check for image file type TypeId deType = fileConverter.DetectedTypeId; if (imageTypes.Contains(deType)) { errObjs.Add(new ErrorDataObject("1002", "Image File Type: " + deType.ToString(), "Warning")); } // return if there is a dtSearch error other than file corrupt (10) or file encrypted (17) JobErrorInfo errorInfo = fileConverter.Errors; bool fatalError = false; bool fileMissingOrNoText = false; int dtErCode = 0; if (errorInfo != null && errorInfo.Count > 0) { for (int i = 0; i < errorInfo.Count; i++) { dtErCode = errorInfo.Code(i); string errorCode = ""; if (dtErCode != 9 && dtErCode != 10 && dtErCode != 17 && dtErCode != 207 && dtErCode != 16 && dtErCode != 21) { errObjs.Add(new ErrorDataObject("1005", "Text extraction Error occurred during processing of the document. " + errorInfo.Message(i), "Error")); fatalError = true; } else { string errText = ""; if (dtErCode == 10) // dtsErFileCorrupt { errorCode = "1013"; errText = "Document is corrupted."; } if (dtErCode == 17) // dtsErFileEncrypted { errorCode = "1007"; errText = "A component of the document is encrypted."; } if (dtErCode == 207) // dtsErContainerItemEncrypted, internal error code { errorCode = "1014"; errText = "The document is encrypted."; string text = errorInfo.Message(i); if (text != null) { int index = text.IndexOf("->"); if (index >= 0) { errText = "A component of the document is encrypted. " + text.Substring(index); } } } if (dtErCode == 9) // dtsErAccFile { errorCode = "1010"; errText = "The system cannot access the file specified."; } if (dtErCode == 16) // dtsErFileNotFound { errorCode = "1011"; errText = "Document file does not exist."; } if (dtErCode == 21) // dtsErFileEmpty { errorCode = "1012"; errText = "Document file is empty"; } if (dtErCode == 9 || dtErCode == 10 || dtErCode == 207 || dtErCode == 16 || dtErCode == 21) { fileMissingOrNoText = true; // file missing, no text, corrupt or encrypted } if (errText == "") { errText = errorInfo.Message(i); } errObjs.Add(new ErrorDataObject(errorCode, "Text extraction error: " + errText, "Warning")); } } } if (fatalError) { errorFlag = false; return; } else { if (fileMissingOrNoText) { return; } if (dtErCode == 17) { FileInfo fi = new FileInfo(targetFileNameDTSearch); if (fi.Length == 0) { errObjs.Clear(); // remove error "1007" errObjs.Add(new ErrorDataObject("1014", "Text extraction error: document is encrypted.", "Warning")); return; } } //load the dtSearch XML output file into an XML document XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(targetFileNameDTSearch); } catch { //try cleaning up the metadata tags and loading again cleanMetadataTags(targetFileNameDTSearch); xmlDoc.Load(targetFileNameDTSearch); } //start with the document node XmlNode docNode = xmlDoc.DocumentElement; //initialize the output strings outStringIndex.Length = 0; outStringIndex.AppendLine("DocID: " + docID); if (title != null && title.Length > 0) { outStringIndex.AppendLine("Filename: " + title); } outStringAnalysis.Length = 0; //start outputting with the document node outputNode(docNode, outStringIndex, outStringAnalysis, errObjs); indexText = outStringIndex.ToString(); analysisText = outStringAnalysis.ToString(); //signal error if no analysis text if (analysisText.Length == 0) { errObjs.Add(new ErrorDataObject("1003", "No text to analyze", "Warning")); } return; } } catch (Exception ex) { errObjs.Add(new ErrorDataObject("1001", "Text extraction Error occurred during processing of the document. " + ex.Message, "Error")); errorFlag = false; return; } }
public async Task Set(File file) { Clear(); labelName.Text = file.FileName; labelType.Text = DirectoryContent.ToString(file.Type); string additionalInfo = null; switch (file.Type) { case FileType.TextFile: { icon.Image = new Icon(Properties.Resources.Rtf, new Size(64, 64)).ToBitmap(); previewContent.Text = await((TextFile)file).DownloadAsString(); previewContent.Visible = true; break; } case FileType.SoundFile: { icon.Image = new Icon(Properties.Resources.Rsf, new Size(64, 64)).ToBitmap(); break; } case FileType.GraphicFile: { icon.Image = new Icon(Properties.Resources.Rgf, new Size(64, 64)).ToBitmap(); byte[] data = await file.Download(); Bitmap bitmap = FileConverter.RGFtoBitmap(data, Color.FromArgb(73, 74, 75)); int width = data[0]; int height = data[1]; previewImage.Image = bitmap; additionalInfo = $"Dimension: {width} x {height} px"; previewImage.Visible = true; break; } default: { icon.Image = new Icon(Properties.Resources.File, new Size(64, 64)).ToBitmap(); break; } } StringBuilder sb = new StringBuilder(); if (UserSettings.Mode == Mode.BASIC) { sb.AppendLine($"Size: {DirectoryContent.ToFileSize(file.Size)}"); } else { sb.AppendLine($"Path: {file.Path}"); sb.AppendLine($"Size: {DirectoryContent.ToByteFileSize(file.Size)}"); sb.AppendLine($"Md5sum: {file.MD5SUM}"); } if (additionalInfo != null) { sb.Append(additionalInfo); } textBoxInfo.Text = sb.ToString(); }
private void PageLoad() { var editPossible = !RequestEmbedded && !IsMobile; var isExtenral = false; File file; var fileUri = string.Empty; if (!ItsTry) { try { if (string.IsNullOrEmpty(RequestFileUrl)) { _fileNew = !string.IsNullOrEmpty(Request[UrlConstant.New]) && Request[UrlConstant.New] == "true"; var ver = string.IsNullOrEmpty(Request[CommonLinkUtility.Version]) ? -1 : Convert.ToInt32(Request[CommonLinkUtility.Version]); file = DocumentServiceHelper.GetParams(RequestFileId, ver, RequestShareLinkKey, _fileNew, editPossible, !RequestView, out _docParams); _fileNew = file.Version == 1 && file.ConvertedType != null && _fileNew && file.CreateOn == file.ModifiedOn; } else { isExtenral = true; fileUri = RequestFileUrl; var fileTitle = Request[CommonLinkUtility.FileTitle]; if (string.IsNullOrEmpty(fileTitle)) { fileTitle = Path.GetFileName(HttpUtility.UrlDecode(fileUri)) ?? ""; } if (CoreContext.Configuration.Standalone) { try { var webRequest = WebRequest.Create(RequestFileUrl); using (var response = webRequest.GetResponse()) using (var responseStream = new ResponseStream(response)) { fileUri = DocumentServiceConnector.GetExternalUri(responseStream, MimeMapping.GetMimeMapping(fileTitle), "new"); } } catch (Exception error) { Global.Logger.Error("Cannot receive external url for \"" + RequestFileUrl + "\"", error); } } file = new File { ID = fileUri.GetHashCode(), Title = Global.ReplaceInvalidCharsAndTruncate(fileTitle) }; file = DocumentServiceHelper.GetParams(file, true, true, true, false, false, false, out _docParams); _docParams.CanEdit = editPossible && !CoreContext.Configuration.Standalone; _editByUrl = true; _docParams.FileUri = fileUri; } } catch (Exception ex) { _errorMessage = ex.Message; return; } } else { FileType tryType; try { tryType = (FileType)Enum.Parse(typeof(FileType), Request[CommonLinkUtility.TryParam]); } catch { tryType = FileType.Document; } var fileTitle = "Demo"; fileTitle += FileUtility.InternalExtension[tryType]; var relativeUri = string.Format(CommonLinkUtility.FileHandlerPath + UrlConstant.ParamsDemo, tryType); fileUri = new Uri(Request.Url, relativeUri).ToString(); file = new File { ID = Guid.NewGuid(), Title = Global.ReplaceInvalidCharsAndTruncate(fileTitle) }; file = DocumentServiceHelper.GetParams(file, true, true, true, editPossible, editPossible, true, out _docParams); _docParams.FileUri = fileUri; _editByUrl = true; } if (_docParams.ModeWrite && FileConverter.MustConvert(file)) { try { file = FileConverter.ExecDuplicate(file, RequestShareLinkKey); } catch (Exception e) { _docParams = null; _errorMessage = e.Message; return; } var comment = "#message/" + HttpUtility.UrlEncode(FilesCommonResource.CopyForEdit); Response.Redirect(CommonLinkUtility.GetFileWebEditorUrl(file.ID) + comment); return; } Title = HeaderStringHelper.GetPageTitle(file.Title); if (string.IsNullOrEmpty(_docParams.FolderUrl)) { _docParams.FolderUrl = Request[CommonLinkUtility.FolderUrl] ?? ""; } if (MobileDetector.IsRequestMatchesMobile(Context.Request.UserAgent, true)) { _docParams.FolderUrl = string.Empty; } if (RequestEmbedded) { _docParams.Type = DocumentServiceParams.EditorType.Embedded; var shareLinkParam = "&" + CommonLinkUtility.DocShareKey + "=" + RequestShareLinkKey; _docParams.ViewerUrl = CommonLinkUtility.GetFullAbsolutePath(CommonLinkUtility.FilesBaseAbsolutePath + CommonLinkUtility.EditorPage + "?" + CommonLinkUtility.Action + "=view" + shareLinkParam); _docParams.DownloadUrl = CommonLinkUtility.GetFullAbsolutePath(CommonLinkUtility.FileHandlerPath + "?" + CommonLinkUtility.Action + "=download" + shareLinkParam); _docParams.EmbeddedUrl = CommonLinkUtility.GetFullAbsolutePath(CommonLinkUtility.FilesBaseAbsolutePath + CommonLinkUtility.EditorPage + "?" + CommonLinkUtility.Action + "=embedded" + shareLinkParam); } else { _docParams.Type = IsMobile ? DocumentServiceParams.EditorType.Mobile : DocumentServiceParams.EditorType.Desktop; if (FileSharing.CanSetAccess(file)) { _docParams.SharingSettingsUrl = CommonLinkUtility.GetFullAbsolutePath(CommonLinkUtility.FilesBaseAbsolutePath + "share.aspx" + "?" + CommonLinkUtility.FileId + "=" + file.ID + "&" + CommonLinkUtility.FileTitle + "=" + HttpUtility.UrlEncode(file.Title)); } } if (!isExtenral) { _docKeyForTrack = DocumentServiceHelper.GetDocKey(file.ID, -1, DateTime.MinValue); if (!ItsTry) { FileMarker.RemoveMarkAsNew(file); } } if (_docParams.ModeWrite) { _tabId = FileLocker.Add(file.ID, _fileNew); _lockVersion = FileLocker.LockVersion(file.ID); if (ItsTry) { AppendAuthControl(); } } else { _docParams.LinkToEdit = _editByUrl ? CommonLinkUtility.GetFullAbsolutePath(string.Format(CommonLinkUtility.FileWebEditorExternalUrlString, HttpUtility.UrlEncode(fileUri), file.Title)) : FileConverter.MustConvert(_docParams.File) ? CommonLinkUtility.GetFullAbsolutePath(CommonLinkUtility.GetFileWebEditorUrl(file.ID)) : string.Empty; } if (CoreContext.Configuration.YourDocsDemo && IsMobile) { _docParams.CanEdit = false; } }
/// <summary> /// Downloads this soundfile from the Brick and returns this file as WAV byte[] /// </summary> /// <returns>byte[] data of the file</returns> public async Task <byte[]> DownloadAsWAV() { byte[] rsf = await Download(); return(await FileConverter.RSFtoWAV(rsf)); }
private void PageLoad() { var editPossible = !RequestEmbedded; var isExtenral = false; File file; var fileUri = string.Empty; try { if (string.IsNullOrEmpty(RequestFileUrl)) { var app = ThirdPartySelector.GetAppByFileId(RequestFileId); if (app == null) { file = DocumentServiceHelper.GetParams(RequestFileId, RequestVersion, RequestShareLinkKey, editPossible, !RequestView, true, out _configuration); if (_valideShareLink) { _configuration.Document.SharedLinkKey += RequestShareLinkKey; if (CoreContext.Configuration.Personal && !SecurityContext.IsAuthenticated) { var user = CoreContext.UserManager.GetUsers(file.CreateBy); var culture = CultureInfo.GetCultureInfo(user.CultureName); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; } } } else { isExtenral = true; bool editable; _thirdPartyApp = true; file = app.GetFile(RequestFileId, out editable); file = DocumentServiceHelper.GetParams(file, true, editPossible ? FileShare.ReadWrite : FileShare.Read, false, editable, editable, editable, true, out _configuration); _configuration.Document.Url = app.GetFileStreamUrl(file); _configuration.EditorConfig.Customization.GobackUrl = string.Empty; } } else { isExtenral = true; fileUri = RequestFileUrl; var fileTitle = Request[FilesLinkUtility.FileTitle]; if (string.IsNullOrEmpty(fileTitle)) { fileTitle = Path.GetFileName(HttpUtility.UrlDecode(fileUri)) ?? ""; } file = new File { ID = RequestFileUrl, Title = Global.ReplaceInvalidCharsAndTruncate(fileTitle) }; file = DocumentServiceHelper.GetParams(file, true, FileShare.Read, false, false, false, false, false, out _configuration); _configuration.Document.Permissions.Edit = editPossible && !CoreContext.Configuration.Standalone; _configuration.Document.Permissions.Rename = false; _configuration.Document.Permissions.Review = false; _configuration.Document.Permissions.FillForms = false; _configuration.Document.Permissions.ChangeHistory = false; _configuration.Document.Permissions.ModifyFilter = false; _editByUrl = true; _configuration.Document.Url = fileUri; } ErrorMessage = _configuration.ErrorMessage; } catch (Exception ex) { Global.Logger.Warn("DocEditor", ex); ErrorMessage = ex.Message; return; } if (_configuration.EditorConfig.ModeWrite && FileConverter.MustConvert(file)) { try { file = FileConverter.ExecSync(file, RequestShareLinkKey); } catch (Exception ex) { _configuration = null; Global.Logger.Error("DocEditor", ex); ErrorMessage = ex.Message; return; } var comment = "#message/" + HttpUtility.UrlEncode(string.Format(FilesCommonResource.ConvertForEdit, file.Title)); Response.Redirect(FilesLinkUtility.GetFileWebEditorUrl(file.ID) + comment); return; } Title = file.Title + GetPageTitlePostfix(); if (_configuration.EditorConfig.Customization.Goback == null || string.IsNullOrEmpty(_configuration.EditorConfig.Customization.Goback.Url)) { _configuration.EditorConfig.Customization.GobackUrl = Request[FilesLinkUtility.FolderUrl] ?? ""; } _configuration.EditorConfig.Customization.IsRetina = TenantLogoManager.IsRetina(Request); if (RequestEmbedded) { _configuration.Type = Services.DocumentService.Configuration.EditorType.Embedded; _configuration.EditorConfig.Embedded.ShareLinkParam = string.IsNullOrEmpty(RequestShareLinkKey) ? string.Empty : "&" + FilesLinkUtility.DocShareKey + "=" + RequestShareLinkKey; } else { _configuration.Type = IsMobile ? Services.DocumentService.Configuration.EditorType.Mobile : Services.DocumentService.Configuration.EditorType.Desktop; if (FileSharing.CanSetAccess(file) && !(file.Encrypted && (!Request.DesktopApp() || CoreContext.Configuration.Personal))) { _configuration.EditorConfig.SharingSettingsUrl = CommonLinkUtility.GetFullAbsolutePath( Share.Location + "?" + FilesLinkUtility.FileId + "=" + HttpUtility.UrlEncode(file.ID.ToString()) + (Request.DesktopApp() ? "&desktop=true" : string.Empty)); } if (file.RootFolderType == FolderType.Privacy) { if (!PrivacyRoomSettings.Enabled) { _configuration = null; ErrorMessage = FilesCommonResource.ErrorMassage_FileNotFound; return; } else { if (Request.DesktopApp()) { var keyPair = EncryptionKeyPair.GetKeyPair(); if (keyPair != null) { _configuration.EditorConfig.EncryptionKeys = new Services.DocumentService.Configuration.EditorConfiguration.EncryptionKeysConfig { PrivateKeyEnc = keyPair.PrivateKeyEnc, PublicKey = keyPair.PublicKey, }; } } } } } if (!isExtenral) { _docKeyForTrack = DocumentServiceHelper.GetDocKey(file.ID, -1, DateTime.MinValue); FileMarker.RemoveMarkAsNew(file); if (!file.Encrypted && !file.ProviderEntry) { EntryManager.MarkAsRecent(file); } } if (SecurityContext.IsAuthenticated) { _configuration.EditorConfig.SaveAsUrl = CommonLinkUtility.GetFullAbsolutePath(SaveAs.GetUrl); } if (_configuration.EditorConfig.ModeWrite) { _tabId = FileTracker.Add(file.ID); Global.SocketManager.FilesChangeEditors(file.ID); if (SecurityContext.IsAuthenticated) { _configuration.EditorConfig.FileChoiceUrl = CommonLinkUtility.GetFullAbsolutePath(FileChoice.GetUrlForEditor); } } else { _linkToEdit = _editByUrl ? CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorExternalUrl(fileUri, file.Title)) : CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorUrl(file.ID)); if (Request.DesktopApp()) { _linkToEdit += "&desktop=true"; } if (FileConverter.MustConvert(_configuration.Document.Info.File)) { _editByUrl = true; } } var actionAnchor = Request[FilesLinkUtility.Anchor]; if (!string.IsNullOrEmpty(actionAnchor)) { _configuration.EditorConfig.ActionLinkString = actionAnchor; } }
private void PageLoad() { var editPossible = !RequestEmbedded; var isExtenral = false; File file; var fileUri = string.Empty; try { if (string.IsNullOrEmpty(RequestFileUrl)) { var app = ThirdPartySelector.GetAppByFileId(RequestFileId); if (app == null) { var ver = string.IsNullOrEmpty(Request[FilesLinkUtility.Version]) ? -1 : Convert.ToInt32(Request[FilesLinkUtility.Version]); file = DocumentServiceHelper.GetParams(RequestFileId, ver, RequestShareLinkKey, editPossible, !RequestView, true, out _configuration); } else { isExtenral = true; bool editable; _thirdPartyApp = true; file = app.GetFile(RequestFileId, out editable); file = DocumentServiceHelper.GetParams(file, true, editPossible ? FileShare.ReadWrite : FileShare.Read, false, editable, editable, editable, true, out _configuration); _configuration.Document.Url = app.GetFileStreamUrl(file); _configuration.EditorConfig.Customization.GobackUrl = string.Empty; } } else { isExtenral = true; fileUri = RequestFileUrl; var fileTitle = Request[FilesLinkUtility.FileTitle]; if (string.IsNullOrEmpty(fileTitle)) { fileTitle = Path.GetFileName(HttpUtility.UrlDecode(fileUri)) ?? ""; } file = new File { ID = RequestFileUrl, Title = Global.ReplaceInvalidCharsAndTruncate(fileTitle) }; file = DocumentServiceHelper.GetParams(file, true, FileShare.Read, false, false, false, false, false, out _configuration); _configuration.Document.Permissions.Edit = editPossible && !CoreContext.Configuration.Standalone; _configuration.Document.Permissions.Rename = false; _configuration.Document.Permissions.Review = false; _configuration.Document.Permissions.ChangeHistory = false; _editByUrl = true; _configuration.Document.Url = fileUri; } ErrorMessage = _configuration.ErrorMessage; } catch (Exception ex) { Global.Logger.Warn("DocEditor", ex); ErrorMessage = ex.Message; return; } if (_configuration.EditorConfig.ModeWrite && FileConverter.MustConvert(file)) { try { file = FileConverter.ExecDuplicate(file, RequestShareLinkKey); } catch (Exception ex) { _configuration = null; Global.Logger.Error("DocEditor", ex); ErrorMessage = ex.Message; return; } var comment = "#message/" + HttpUtility.UrlEncode(FilesCommonResource.CopyForEdit); Response.Redirect(FilesLinkUtility.GetFileWebEditorUrl(file.ID) + comment); return; } Title = file.Title; if (_configuration.EditorConfig.Customization.Goback == null || string.IsNullOrEmpty(_configuration.EditorConfig.Customization.Goback.Url)) { _configuration.EditorConfig.Customization.GobackUrl = Request[FilesLinkUtility.FolderUrl] ?? ""; } _configuration.EditorConfig.Customization.IsRetina = TenantLogoManager.IsRetina(Request); if (RequestEmbedded) { _configuration.Type = Services.DocumentService.Configuration.EditorType.Embedded; _configuration.EditorConfig.Embedded.ShareLinkParam = string.IsNullOrEmpty(RequestShareLinkKey) ? string.Empty : "&" + FilesLinkUtility.DocShareKey + "=" + RequestShareLinkKey; } else { _configuration.Type = IsMobile ? Services.DocumentService.Configuration.EditorType.Mobile : Services.DocumentService.Configuration.EditorType.Desktop; if (FileSharing.CanSetAccess(file)) { _configuration.EditorConfig.SharingSettingsUrl = CommonLinkUtility.GetFullAbsolutePath(Share.Location + "?" + FilesLinkUtility.FileId + "=" + HttpUtility.UrlEncode(file.ID.ToString())); } } if (!isExtenral) { _docKeyForTrack = DocumentServiceHelper.GetDocKey(file.ID, -1, DateTime.MinValue); FileMarker.RemoveMarkAsNew(file); } if (_configuration.EditorConfig.ModeWrite) { _tabId = FileTracker.Add(file.ID); Global.SocketManager.FilesChangeEditors(file.ID); if (SecurityContext.IsAuthenticated) { _configuration.EditorConfig.FileChoiceUrl = CommonLinkUtility.GetFullAbsolutePath(FileChoice.Location) + "?" + FileChoice.ParamFilterExt + "=xlsx&" + FileChoice.MailMergeParam + "=true"; _configuration.EditorConfig.MergeFolderUrl = CommonLinkUtility.GetFullAbsolutePath(MailMerge.GetUrl); } } else { _linkToEdit = _editByUrl ? CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorExternalUrl(fileUri, file.Title)) : CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorUrl(file.ID)); if (FileConverter.MustConvert(_configuration.Document.Info.File)) { _editByUrl = true; } } }
/// <summary> /// input in program /// </summary> /// <param name="args"></param> private static void Main(string[] args) { try { SettingsModel settings = new SettingsModel(); OcrRequestModel requestModel = new OcrRequestModel(); string lang = "English"; //init model requestModel.ApiKey = settings.ApiKey; requestModel.CleanupSettings = new CleanupSettingsModel() { Deskew = true, RemoveGarbage = true, RemoveTexture = true, SplitDualPage = true, RotationType = "NoRotation", JpegQuality = "", OutputFormat = "", Resolution = "" }; requestModel.OcrSettings = new OcrSettingsModel() { SpeedOcr = false, LookForBarcodes = false, AnalysisMode = "MixedDocument", PrintType = "Print", OcrLanguage = lang }; requestModel.OutputSettings = new OutputSettingsModel() { ExportFormat = "Text;PDF" }; requestModel.InputFiles.Add(new InputFileModel() { Name = Path.GetFileName(settings.TestEnglishFile), Password = "", InputUrl = "", InputBlob = FileConverter.ConvertFileToBase64(settings.TestEnglishFile), InputType = "JPG", PostFix = "" }); //end init String url = "http://api.ocr-it.com:40000/api/jobs"; var json = JsonConvert.SerializeObject(requestModel); AssistProcessor processor = new AssistProcessor(); //processor.MakeOcr(url,json); string baseUrl = "http://api.ocr-it.com:40000/api/Jobs?JobId="; string jobId = "12dea613-f6f3-4982-baa0-60e4f452ae4b"; string gUrl = baseUrl + jobId; string jobStatus = processor.GetJobStatus(gUrl); OcrResponseModel desModel = JsonConvert.DeserializeObject <OcrResponseModel>(jobStatus); string downloadDir = "d://OCRDownload//"; foreach (var file in desModel.Download) { string sName = DateTime.Now.ToString(); sName = sName.Replace(":", string.Empty); sName = sName.Replace("-", string.Empty); sName = sName.Replace(".", string.Empty); sName = sName.Replace(",", string.Empty); sName = sName.Replace(";", string.Empty); sName = sName.Replace(" ", string.Empty); string ext = ""; switch (file.OutputFormat) { case "PDF": ext = "pdf"; break; case "Text": ext = "txt"; break; } sName = sName + "." + ext; string downloadPath = Path.Combine(downloadDir, sName); // processor.DownloadFile(file.Uri,downloadPath); } //string outReq = JsonConvert.SerializeObject(model); Console.ReadKey(); } catch (Exception exception) { Console.WriteLine("Exception: " + exception.Message); Console.ReadKey(); } }
public async Task <bool> Save() { errorProvider.Clear(); if (string.IsNullOrWhiteSpace(textBoxFileName.Text)) { errorProvider.SetError(textBoxFileName, "Filename is required"); return(false); } string fileName = System.IO.Path.GetFileNameWithoutExtension(textBoxFileName.Text.Trim()); bool isNewName = !fileName.Equals(originalFileName, StringComparison.InvariantCultureIgnoreCase); if (!TextHasChanged && !isNewName) { return(true); } //textbox returns lines as /n .. weird should ne /r/n //so hack work around string[] lines = richTextBox.Lines; string text = "-"; if (lines?.Length > 0) { StringBuilder sb = new StringBuilder(); int lineCount = lines.Length; for (int i = 0; i < lineCount; i++) { if (i + 1 < lineCount) { sb.AppendLine(lines[i]); } else { sb.Append(lines[i]); } } text = sb.ToString(); } byte[] data = FileConverter.TexttoRTF(text); if (TextFile != null) { await BrickExplorer.UploadFile(data, Directory.Path, $"{fileName}.rtf"); if (isNewName) { await TextFile.Delete(); } } else { await BrickExplorer.UploadFile(data, Directory.Path, $"{fileName}.rtf"); } TextHasChanged = false; RefreshDirectory = true; return(true); }
protected File PerformCrossDaoFileCopy(object fromFileId, object toFolderId, bool deleteSourceFile) { var fromSelector = GetSelector(fromFileId); var toSelector = GetSelector(toFolderId); //Get File from first dao var fromFileDao = fromSelector.GetFileDao(fromFileId); var toFileDao = toSelector.GetFileDao(toFolderId); var fromFile = fromFileDao.GetFile(fromSelector.ConvertId(fromFileId)); if (fromFile.ContentLength > SetupInfo.AvailableFileSize) { throw new Exception(string.Format(deleteSourceFile ? FilesCommonResource.ErrorMassage_FileSizeMove : FilesCommonResource.ErrorMassage_FileSizeCopy, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize))); } using (var securityDao = TryGetSecurityDao()) using (var tagDao = TryGetTagDao()) { var fromFileShareRecords = securityDao.GetPureShareRecords(fromFile).Where(x => x.EntryType == FileEntryType.File); var fromFileNewTags = tagDao.GetNewTags(Guid.Empty, fromFile).ToList(); var fromFileLockTag = tagDao.GetTags(fromFile.ID, FileEntryType.File, TagType.Locked).FirstOrDefault(); var fromFileFavoriteTag = tagDao.GetTags(fromFile.ID, FileEntryType.File, TagType.Favorite); var fromFileTemplateTag = tagDao.GetTags(fromFile.ID, FileEntryType.File, TagType.Template); var toFile = new File { Title = fromFile.Title, Encrypted = fromFile.Encrypted, FolderID = toSelector.ConvertId(toFolderId) }; fromFile.ID = fromSelector.ConvertId(fromFile.ID); var mustConvert = !string.IsNullOrEmpty(fromFile.ConvertedType); using (var fromFileStream = mustConvert ? FileConverter.Exec(fromFile) : fromFileDao.GetFileStream(fromFile)) { toFile.ContentLength = fromFileStream.CanSeek ? fromFileStream.Length : fromFile.ContentLength; toFile = toFileDao.SaveFile(toFile, fromFileStream); } if (deleteSourceFile) { if (fromFileShareRecords.Any()) { fromFileShareRecords.ToList().ForEach(x => { x.EntryId = toFile.ID; securityDao.SetShare(x); }); } var fromFileTags = fromFileNewTags; if (fromFileLockTag != null) { fromFileTags.Add(fromFileLockTag); } if (fromFileFavoriteTag != null) { fromFileTags.AddRange(fromFileFavoriteTag); } if (fromFileTemplateTag != null) { fromFileTags.AddRange(fromFileTemplateTag); } if (fromFileTags.Any()) { fromFileTags.ForEach(x => x.EntryId = toFile.ID); tagDao.SaveTags(fromFileTags); } //Delete source file if needed fromFileDao.DeleteFile(fromSelector.ConvertId(fromFileId)); } return(toFile); } }
private void PageLoad() { var editPossible = !RequestEmbedded; var isExtenral = false; File file; var fileUri = string.Empty; try { if (string.IsNullOrEmpty(RequestFileUrl)) { _fileNew = (Request["new"] ?? "") == "true"; var app = ThirdPartySelector.GetAppByFileId(RequestFileId); if (app == null) { var ver = string.IsNullOrEmpty(Request[FilesLinkUtility.Version]) ? -1 : Convert.ToInt32(Request[FilesLinkUtility.Version]); file = DocumentServiceHelper.GetParams(RequestFileId, ver, RequestShareLinkKey, _fileNew, editPossible, !RequestView, out _docParams); _fileNew = _fileNew && file.Version == 1 && file.CreateOn == file.ModifiedOn; } else { isExtenral = true; bool editable; _thirdPartyApp = true; file = app.GetFile(RequestFileId, out editable); file = DocumentServiceHelper.GetParams(file, true, true, true, editable, editable, editable, editable, out _docParams); _docParams.FileUri = app.GetFileStreamUrl(file); _docParams.FolderUrl = string.Empty; } } else { isExtenral = true; fileUri = RequestFileUrl; var fileTitle = Request[FilesLinkUtility.FileTitle]; if (string.IsNullOrEmpty(fileTitle)) { fileTitle = Path.GetFileName(HttpUtility.UrlDecode(fileUri)) ?? ""; } if (CoreContext.Configuration.Standalone) { try { var webRequest = (HttpWebRequest)WebRequest.Create(RequestFileUrl); // hack. http://ubuntuforums.org/showthread.php?t=1841740 if (WorkContext.IsMono) { ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true; } using (var response = webRequest.GetResponse()) using (var responseStream = new ResponseStream(response)) { var externalFileKey = DocumentServiceConnector.GenerateRevisionId(RequestFileUrl); fileUri = DocumentServiceConnector.GetExternalUri(responseStream, MimeMapping.GetMimeMapping(fileTitle), externalFileKey); } } catch (Exception error) { Global.Logger.Error("Cannot receive external url for \"" + RequestFileUrl + "\"", error); } } file = new File { ID = RequestFileUrl, Title = Global.ReplaceInvalidCharsAndTruncate(fileTitle) }; file = DocumentServiceHelper.GetParams(file, true, true, true, false, false, false, false, out _docParams); _docParams.CanEdit = editPossible && !CoreContext.Configuration.Standalone; _docParams.CanReview = _docParams.CanEdit; _editByUrl = true; _docParams.FileUri = fileUri; } } catch (Exception ex) { Global.Logger.Error("DocEditor", ex); _errorMessage = ex.Message; return; } if (_docParams.ModeWrite && FileConverter.MustConvert(file)) { try { file = FileConverter.ExecDuplicate(file, RequestShareLinkKey); } catch (Exception ex) { _docParams = null; Global.Logger.Error("DocEditor", ex); _errorMessage = ex.Message; return; } var comment = "#message/" + HttpUtility.UrlEncode(FilesCommonResource.CopyForEdit); Response.Redirect(FilesLinkUtility.GetFileWebEditorUrl(file.ID) + comment); return; } Title = file.Title; if (string.IsNullOrEmpty(_docParams.FolderUrl)) { _docParams.FolderUrl = Request[FilesLinkUtility.FolderUrl] ?? ""; } if (MobileDetector.IsRequestMatchesMobile(true)) { _docParams.FolderUrl = string.Empty; } if (RequestEmbedded) { _docParams.Type = DocumentServiceParams.EditorType.Embedded; var shareLinkParam = "&" + FilesLinkUtility.DocShareKey + "=" + RequestShareLinkKey; _docParams.ViewerUrl = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.FilesBaseAbsolutePath + FilesLinkUtility.EditorPage + "?" + FilesLinkUtility.Action + "=view" + shareLinkParam); _docParams.DownloadUrl = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.FileHandlerPath + "?" + FilesLinkUtility.Action + "=download" + shareLinkParam); _docParams.EmbeddedUrl = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.FilesBaseAbsolutePath + FilesLinkUtility.EditorPage + "?" + FilesLinkUtility.Action + "=embedded" + shareLinkParam); } else { _docParams.Type = IsMobile ? DocumentServiceParams.EditorType.Mobile : DocumentServiceParams.EditorType.Desktop; if (FileSharing.CanSetAccess(file)) { _docParams.SharingSettingsUrl = CommonLinkUtility.GetFullAbsolutePath(Share.Location + "?" + FilesLinkUtility.FileId + "=" + file.ID); } } if (!isExtenral) { _docKeyForTrack = DocumentServiceHelper.GetDocKey(file.ID, -1, DateTime.MinValue); FileMarker.RemoveMarkAsNew(file); } if (_docParams.ModeWrite) { _tabId = FileTracker.Add(file.ID, _fileNew); _fixedVersion = FileTracker.FixedVersion(file.ID); if (SecurityContext.IsAuthenticated) { _docParams.FileChoiceUrl = CommonLinkUtility.GetFullAbsolutePath(FileChoice.Location) + "?" + FileChoice.ParamFilterExt + "=xlsx&" + FileChoice.MailMergeParam + "=true"; _docParams.MergeFolderUrl = CommonLinkUtility.GetFullAbsolutePath(MailMerge.GetUrl); } } else { if (!RequestView && FileTracker.IsEditingAlone(file.ID)) { var editingBy = FileTracker.GetEditingBy(file.ID).FirstOrDefault(); _errorMessage = string.Format(FilesCommonResource.ErrorMassage_EditingMobile, Global.GetUserName(editingBy)); } _docParams.LinkToEdit = _editByUrl ? CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorExternalUrl(fileUri, file.Title)) : CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorUrl(file.ID)); if (FileConverter.MustConvert(_docParams.File)) { _editByUrl = true; } } }
public FileHelper(FileTrackerHelper fileTracker, FilesLinkUtility filesLinkUtility, FileUtility fileUtility, FileConverter fileConverter, Global global) { FileTracker = fileTracker; FilesLinkUtility = filesLinkUtility; FileUtility = fileUtility; FileConverter = fileConverter; Global = global; }