public static ImportRun RunTest(EntityType entityType, string fileName, ImportFormat importFormat, string sheetName = null) { string fileToken; using (Stream stream = SheetTestHelper.GetStream(fileName)) { fileToken = FileRepositoryHelper.AddTemporaryFile(stream); } EntityType type = entityType; ImportConfig importConfig = CreateImportConfig(type, importFormat, sheetName); ImportRun importRun = CreateImportRun(importConfig, fileToken); ISpreadsheetInspector inspector = Factory.Current.Resolve <ISpreadsheetInspector>( ); SpreadsheetInfo info = inspector.GetSpreadsheetInfo(fileToken, importFormat); SampleTable sample = inspector.GetSampleTable(fileToken, importFormat, sheetName, 1, 2, null); AddAllFields(importConfig, sample); // Run import IImportRunWorker worker = Factory.Current.Resolve <IImportRunWorker>( ); worker.StartImport(importRun.Id); return(importRun); }
private static Document CreateAttachment(IEntity incidentEntity, string fileName, string docExt, string content) { //todo - work out how to get the right doc type, although I can't see what to use for images //todo - at the moment this assumes the content is base64... does that make sense?? var docType = ReadiNow.Model.Entity.Get <DocumentType>(new EntityRef("textDocumentDocumentType")); string hash; using (var stream = new MemoryStream(Convert.FromBase64String(content))) { hash = FileRepositoryHelper.AddTemporaryFile(stream); } var doc = ReadiNow.Model.Entity.Create <Document>(); doc.FileDataHash = hash; doc.Name = fileName; doc.FileExtension = docExt; doc.DocumentFileType = docType; doc.SetRelationships("readidesk:attachmentIncident", new EntityRelationshipCollection <IEntity> { incidentEntity }, Direction.Reverse); doc.Save(); return(doc); }
private ReportTemplate CreateTestTemplate(string fileName) { using (var ctx = DatabaseContext.GetContext(requireTransaction: true)) { var wordDocType = Entity.Get <DocumentType>(new EntityRef("wordDocumentDocumentType")); var stream = GetTestDocStream(fileName); //var hash = CryptoHelper.ComputeSha256Hash(stream); var tempHash = FileRepositoryHelper.AddTemporaryFile(stream); var docRev = Entity.Create <DocumentRevision>(); docRev.FileDataHash = tempHash; docRev.Name = fileName; docRev.FileExtension = "docx"; docRev.DocumentFileType = wordDocType; docRev.Save(); var hash = docRev.FileDataHash; var doc = Entity.Create <Document>(); doc.FileDataHash = hash; doc.DocumentHasDocumentRevision.Add(docRev); doc.CurrentDocumentRevision = docRev; doc.FileDataHash = hash; doc.Name = fileName; doc.FileExtension = "docx"; doc.DocumentFileType = wordDocType; var template = Entity.Create <ReportTemplate>(); template.Name = fileName; template.ReportTemplateUsesDocument = doc; ctx.CommitTransaction(); return(template); } }
/// <summary> /// Creates the thumbnail image entity. /// </summary> /// <param name="stream">The stream.</param> /// <param name="originalImageEntity">The original image entity.</param> /// <param name="fileExtension">The file extension.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="scaleEnum">The scale enum.</param> /// <returns></returns> private ThumbnailFileType CreateThumbnailImageEntity(Stream stream, ImageFileType originalImageEntity, string fileExtension, int width, int height, ImageScaleEnum scaleEnum) { using (DatabaseContext context = DatabaseContext.GetContext(true)) { stream.Position = 0; string dataHash = FileRepositoryHelper.AddTemporaryFile(stream); var thumbnailEntity = new ThumbnailFileType( ); string thumbnailName = originalImageEntity.Name; if (string.IsNullOrEmpty(thumbnailName)) { thumbnailName = "Thumbnail"; } else { thumbnailName += " Thumbnail"; } thumbnailEntity.Name = thumbnailName; thumbnailEntity.FileDataHash = dataHash; thumbnailEntity.FileExtension = fileExtension; thumbnailEntity.ImageWidth = width; thumbnailEntity.ImageHeight = height; thumbnailEntity.ThumbnailScaling = scaleEnum; thumbnailEntity.IsThumbnailForImage = originalImageEntity; thumbnailEntity.Save( ); context.CommitTransaction( ); return(thumbnailEntity); } }
public async Task <HttpResponseMessage <FileList> > PostFormData( ) { //TODO: store the original filename along with the file. if (!Request.Content.IsMimeMultipartContent( )) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var provider = new LocalCacheStreamProvider( ); try { await Request.Content.ReadAsMultipartAsync(provider); } catch (Exception ex) { throw new WebArgumentException(string.Format("Invalid file upload request. Request: {0}", Request), ex); } var idMap = new FileList( ); try { string expectedType = Request.RequestUri.ParseQueryString( )["type"]; foreach (var entry in provider.RemoteToLocalFileNameMap) { string remoteFileName = entry.Key; string localFileName = entry.Value; string token; if (!FileRepositoryHelper.CheckFileExtensionIsValid(remoteFileName, expectedType)) { throw new PlatformSecurityException("Disallowed file type."); } using (var source = new FileStream(localFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { token = FileRepositoryHelper.AddTemporaryFile(source); } idMap.Add(new FileListItem { FileName = remoteFileName, Hash = token }); } } finally { // clean up the local files foreach (var entry in provider.RemoteToLocalFileNameMap) { File.Delete(entry.Value); } } return(new HttpResponseMessage <FileList>(idMap)); }
private static void CreateTempFile(out byte[] buffer, out string fileHash) { buffer = new Byte[1]; _rand.NextBytes(buffer); var ms = new MemoryStream(buffer); fileHash = FileRepositoryHelper.AddTemporaryFile(ms); }
/// <summary> /// Generate a doucment /// </summary> /// <param name="templateDoc">The template file to generate from</param> /// <param name="sourceResource">The optional resource to use a data for the generation</param> /// <param name="newName">The name of the generated file</param> /// <param name="newDescription">The name of the generated description</param> /// <param name="timezoneName">the timezone the report will be evaluated in</param> public static Document GenerateDoc(Document templateDoc, IEntity sourceResource, string newName, string newDescription, string timezoneName) { var sourceResourceId = sourceResource != null ? sourceResource.Id : 0; var templateName = templateDoc.Name ?? "[Unnamed]"; var tmpFileName = string.Format("{0:yyyyMMddhhmmssfff}.doc", DateTime.UtcNow); var tmpFilePath = Path.Combine(Path.GetTempPath(), tmpFileName); try { // Generate document using (var templateStream = FileRepositoryHelper.GetFileDataStreamForEntity(templateDoc)) { if (templateStream == null) { throw new WorkflowRunException("Unable to find the template file '{0}'", templateName); } using (Stream targetStream = File.Create(tmpFilePath)) { var genSettings = new GeneratorSettings { WriteDebugFiles = false, ThrowOnError = true, TimeZoneName = timezoneName, SelectedResourceId = sourceResourceId, }; Factory.DocumentGenerator.CreateDocument(templateStream, targetStream, genSettings); // Any gen failures are handled higher up } } string tempHash; using (var source = new FileStream(tmpFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { tempHash = FileRepositoryHelper.AddTemporaryFile(source); } var document = DocHelper.CreateDoc(tempHash, newName, newDescription, templateDoc.FileExtension, templateDoc.DocumentFileType); return(document); } finally { // clean up if (File.Exists(tmpFileName)) { File.Delete(tmpFilePath); } } }
/// <summary> /// Inserts the file to the database. /// </summary> /// <param name="fileUploadId">The file upload identifier that was created by the file manager service.</param> /// <remarks>The file upload ID is used as the primary key for the table that holds the file for the import.</remarks> public string InsertFileToRepository(string fileUploadId) { string token; // Write the data to the stream string documentFilename = FileManagerService.GetFilenameFromFileId(fileUploadId); using (var source = new FileStream(documentFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) { token = FileRepositoryHelper.AddTemporaryFile(source); } File.Delete(documentFilename); return(token); }
/// <summary> /// Fetch a file via FTP, returning a token to the temporary file in the file repository /// </summary> /// <param name="url">The file path, must start with either either ftps:// or sftp:// </param> /// <param name="username"></param> /// <param name="password"></param> /// <returns>A token to the file repository</returns> public string GetToTemporaryFile(string url, string username, string password) { var uri = new Uri(url); string fileToken = null; ConnectAndRun(uri.Host, username, password, (sftp) => { using (var ms = new MemoryStream()) { var filePath = uri.AbsolutePath; sftp.DownloadFile(filePath, ms); fileToken = FileRepositoryHelper.AddTemporaryFile(ms); } }); return(fileToken); }
/// <summary> /// Formats the specified message. /// </summary> /// <param name="message">The message.</param> /// <param name="formattedMessage">The formatted message.</param> /// <param name="inbox">The inbox.</param> public MailMessageFormatterResult Format(MailMessage message, ReceivedEmailMessage formattedMessage, IEntity inbox) { if (message == null || formattedMessage == null || inbox == null) { return(MailMessageFormatterResult.Skip); } formattedMessage.EmFrom = message.From?.ToString( ); formattedMessage.EmTo = message.To.ToString( ); formattedMessage.EmSubject = message.Subject; formattedMessage.EmCC = message.CC.ToString( ); formattedMessage.EmBody = message.Body; var parser = new EmailBodyParser(message.Body); formattedMessage.EmReplyFragment = parser.GetReplySectionFromEmailBodyAsText(); formattedMessage.EmReferences = message.Headers[EmailHelper.ReferencesHeader]; formattedMessage.EmIsHtml = message.IsBodyHtml; formattedMessage.EmReceivedDate = DateTime.UtcNow; formattedMessage.FromInbox = inbox.As <Inbox>( ); foreach (var emailAttachment in message.Attachments) { if (emailAttachment.ContentStream.Length > MaxAttachmentSize) { EDC.ReadiNow.Diagnostics.EventLog.Application.WriteWarning("Skipping attachment in email from '{0}' to '{1}'. Attachment is greater than maximum allowed size", message.From, message.To); continue; } var tempHash = FileRepositoryHelper.AddTemporaryFile(emailAttachment.ContentStream); var file = Entity.Create <FileType>(); file.Name = EnsureValidFileName(emailAttachment.Name); file.Description = "Email Attachment"; file.FileDataHash = tempHash; file.Size = (int)emailAttachment.ContentStream.Length; file.FileExtension = Path.GetExtension(emailAttachment.Name); file.Save(); formattedMessage.EmAttachments.Add(file); EDC.ReadiNow.Diagnostics.EventLog.Application.WriteTrace("Created file '{0}' for attachment in email from '{1}' to {2}.", file.Name, message.From, message.To); } return(MailMessageFormatterResult.Ok); }
public async Task <HttpResponseMessage> Upload([FromUri] string tenant, [FromUri] string apiPath) { try { using (Profiler.Measure("ConnectorController.Upload")) { // Capture file string fileToken; using (Stream fileStream = await Request.Content.ReadAsStreamAsync( )) { fileToken = FileRepositoryHelper.AddTemporaryFile(fileStream); } // Run service // Prepare request ConnectorRequest connectorRequest = new ConnectorRequest { Verb = ConnectorVerb.Post, ApiPath = GetApiPath(apiPath), Payload = null, TenantName = tenant, QueryString = GetQueryString( ), ControllerRootPath = GetControllerAddress(UploadPrefix), FileUploadToken = fileToken }; // Run request ConnectorResponse response = _connectorService.HandleRequest(connectorRequest); // Response return(ConvertResponse(response)); } } catch (WebArgumentException) { throw; } catch (Exception ex) { return(UnhandledException(ex)); } }
/// <summary> /// Fetch a file via FTPS, returning a token to the temporary file in the file repository /// </summary> /// <param name="url">The file path, /// <param name="username"></param> /// <param name="password"></param> /// <returns>A token to the file repository</returns> public string GetToTemporaryFile(string uri, string username, string password) { string fileHash = null; ConnectAndRun(uri, username, password, // setup request => { request.Method = WebRequestMethods.Ftp.DownloadFile; }, // handle response responseStream => { fileHash = FileRepositoryHelper.AddTemporaryFile(responseStream); } ); return(fileHash); }
/// <summary> /// Exports the application to an sqlite database file and loads it into the document library. /// </summary> /// <param name="applicationVersionId">The application version id.</param> /// <returns>A token used to access the export for download.</returns> public string ExportApplication(Guid applicationVersionId) { string token; var appFile = $"{DateTime.Now:yyyyMMddhhmmssfff}.xml"; var db = Path.Combine(Path.GetTempPath( ), appFile); AppManager.ExportAppPackage(applicationVersionId, db, Format.Undefined); try { using (var source = new FileStream(db, FileMode.Open, FileAccess.Read, FileShare.Read)) { token = FileRepositoryHelper.AddTemporaryFile(source); } } finally { File.Delete(db); } return(token); }
/// <summary> /// Export Data for given file format. /// </summary> /// <param name="reportId">Report Id.</param> /// <param name="settings">Report settings</param> /// <param name="format">File format.</param> /// <returns>ExportInfo</returns> public ExportInfo ExportData(long reportId, ReportSettings settings, ExportFormat format) { var exportInfo = new ExportInfo(); //set the page size to maximum. settings.SupportPaging = true; settings.InitialRow = 0; settings.PageSize = 10001; // The maximum number of records can be exported is limited to 10000. //to retrive the report object and run the report, dosen't need the writeable permission. var reportingInterface = new ReportingInterface(); ReportResult reportResult = reportingInterface.RunReport(reportId, settings); // Ensure that the report result contains the report metadata (which is loaded in a security bypass context). // This ensures that when processing the grid data the required metadata is available // and will not throw security exceptions. var metadata = reportResult.Metadata; //Remove the last record if the number of records are more then 10000. List <DataRow> rows; if (reportResult.GridData.Count > 10000) { exportInfo.ResponseMessage = "There are more than 10,000 records in this report. Only the first 10,000 records will be downloaded."; rows = reportResult.GridData.GetRange(0, 10000); } else { rows = reportResult.GridData; } Stream fileStream; // Generate content switch (format) { case ExportFormat.Csv: { byte[] resultText = ExportToCsv.CreateCsvDocument(reportResult, rows); fileStream = new MemoryStream(resultText); break; } case ExportFormat.Excel: { fileStream = ExportToExcel.CreateExcelDocument(reportResult, rows); break; } case ExportFormat.Word: { string reportName = Entity.GetName(reportId); fileStream = ExportToWord.CreateWordDocument(reportResult, rows, reportName); break; } default: throw new ArgumentException("fileFormat"); } //save the file to the database. string hash; using (fileStream) { hash = FileRepositoryHelper.AddTemporaryFile(fileStream); } exportInfo.FileHash = hash; return(exportInfo); }
private static string PersistTemporaryFile(MemoryStream documentStream) { return(FileRepositoryHelper.AddTemporaryFile(documentStream)); }
/// <summary> /// Creates a new image /// </summary> /// <param name="imageFileUploadId">The image file upload id.</param> /// <param name="imageEntityData">The image entity data.</param> /// <param name="fileExtension">The file extension.</param> /// <returns> /// The requested data. /// </returns> /// <exception cref="System.NotImplementedException"></exception> public EntityRef CreateImageFromUploadedFile(string imageFileUploadId, EntityData imageEntityData, string fileExtension) { if (string.IsNullOrEmpty(imageFileUploadId)) { throw new ArgumentException(@"The imageFileUploadId is empty.", "imageFileUploadId"); } if (imageEntityData == null) { throw new ArgumentNullException(@"imageEntityData"); } if (string.IsNullOrEmpty(fileExtension)) { throw new ArgumentNullException(@"fileExtension"); } ImageFileType imageFile; string filePath = string.Empty; try { // Create a new image entity #pragma warning disable 618 var entityInfoService = new EntityInfoService( ); #pragma warning restore 618 imageFile = entityInfoService.CreateEntity(imageEntityData).Entity.AsWritable <ImageFileType>( ); var fileManagerService = new FileManagerService( ); FileDetail fileDetails = fileManagerService.GetFileDetails(imageFileUploadId); filePath = fileDetails.FullName; int width; int height; using (Image image = Image.FromFile(fileDetails.FullName)) { width = image.Width; height = image.Height; } string token; using (var source = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { token = FileRepositoryHelper.AddTemporaryFile(source); } imageFile.FileDataHash = token; imageFile.FileExtension = FileHelper.GetFileExtension(filePath); imageFile.ImageWidth = width; imageFile.ImageHeight = height; imageFile.Save( ); } finally { // Delete the underlying temp file if (!string.IsNullOrEmpty(filePath)) { File.Delete(filePath); } } return(imageFile); }