Esempio n. 1
0
        public static string CreateCsvReport(IEnumerable <AuditEvent> events, string reportName)
        {
            try
            {
                using (var stream = new MemoryStream())
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer))
                        {
                            csv.Configuration.RegisterClassMap <AuditEventMap>();

                            csv.WriteHeader <AuditEvent>();
                            foreach (var evt in events)
                            {
                                csv.WriteRecord(evt);
                            }

                            writer.Flush();

                            var file    = FileUploader.Exec(Global.FolderMy.ToString(), reportName, stream.Length, stream, true);
                            var fileUrl = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorUrl((int)file.ID));

                            fileUrl += string.Format("&options={{\"delimiter\":{0},\"codePage\":{1}}}",
                                                     (int)FileUtility.CsvDelimiter.Comma,
                                                     Encoding.UTF8.CodePage);
                            return(fileUrl);
                        }
            }
            catch (Exception ex)
            {
                Log.Error("Error while generating audit report: " + ex);
                throw;
            }
        }
Esempio n. 2
0
        public string CreateLoginHistoryReport()
        {
            SecurityContext.DemandPermissions(SecutiryConstants.EditPortalSettings);

            string fileUrl;

            var to   = DateTime.UtcNow;
            var from = to.AddMonths(-6);

            var events     = LoginEventsRepository.Get(CurrentTenant, from, to);
            var reportPath = AuditReportCreator.CreateXlsxReport(events);

            try
            {
                var reportName = string.Format(AuditReportResource.LoginHistoryReportName + ".xlsx", from.ToString("MM.dd.yyyy"), to.ToString("MM.dd.yyyy"));
                using (var stream = new FileStream(reportPath, FileMode.Open))
                {
                    var file = FileUploader.Exec(Global.FolderMy.ToString(), reportName, stream.Length, stream, true);
                    fileUrl = FilesLinkUtility.GetFileWebEditorUrl((int)file.ID);
                }
            }
            finally
            {
                AuditReportCreator.DeleteReport(reportPath);
            }

            MessageService.Send(context, MessageAction.LoginHistoryReportDownloaded);

            return(fileUrl);
        }
Esempio n. 3
0
        public string CreateCsvReport <TEvent>(IEnumerable <TEvent> events, string reportName) where TEvent : BaseEvent
        {
            try
            {
                using (var stream = new MemoryStream())
                    using (var writer = new StreamWriter(stream, Encoding.UTF8))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            csv.Configuration.RegisterClassMap(new BaseEventMap <TEvent>());

                            csv.WriteHeader <TEvent>();
                            csv.NextRecord();
                            csv.WriteRecords(events);
                            writer.Flush();

                            var file    = FileUploader.Exec(GlobalFolderHelper.FolderMy, reportName, stream.Length, stream, true);
                            var fileUrl = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorUrl(file.ID));

                            fileUrl += string.Format("&options={{\"delimiter\":{0},\"codePage\":{1}}}",
                                                     (int)FileUtility.CsvDelimiter.Comma,
                                                     Encoding.UTF8.CodePage);
                            return(fileUrl);
                        }
            }
            catch (Exception ex)
            {
                Log.Error("Error while generating login report: " + ex);
                throw;
            }
        }
Esempio n. 4
0
 private static void CreateFile(string folderid, string title, string content, string contentType, string extension)
 {
     using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
     {
         FileUploader.Exec(folderid,
                           title.EndsWith(extension, StringComparison.OrdinalIgnoreCase) ? title : (title + extension),
                           memStream.Length, memStream);
     }
 }
        private FileWrapper <T> CreateFile(T folderId, string title, string content, string extension)
        {
            using var memStream = new MemoryStream(Encoding.UTF8.GetBytes(content));
            var file = FileUploader.Exec(folderId,
                                         title.EndsWith(extension, StringComparison.OrdinalIgnoreCase) ? title : (title + extension),
                                         memStream.Length, memStream);

            return(FileWrapperHelper.Get(file));
        }
Esempio n. 6
0
        private void OutputData(string result, string reportName, Report report, ReportViewType outputFormat)
        {
            switch (outputFormat)
            {
            case ReportViewType.Html:
                reportResult.Text = result;

                var sb = new StringBuilder();
                sb.Append("<div class='report-name'>");
                sb.Append(reportName);
                sb.Append("<span class='generation-date'> (");
                sb.Append(TenantUtil.DateTimeNow().ToString(DateTimeExtension.ShortDatePattern));
                sb.Append(")</span>");
                sb.Append("</div>");
                reportFilter.Text = sb.ToString();
                break;

            case ReportViewType.Xml:
            case ReportViewType.EMail:
                if (result != null)
                {
                    var ext = outputFormat.ToString().ToLower();
                    Response.Clear();
                    Response.ContentType     = "text/" + ext + "; charset=utf-8";
                    Response.ContentEncoding = Encoding.UTF8;
                    Response.Charset         = Encoding.UTF8.WebName;
                    Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}.{1}", report.FileName, ext));
                    Response.Write(result);
                    Response.End();
                }
                break;

            case ReportViewType.Csv:
                string fileURL;

                using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(result)))
                {
                    var file = FileUploader.Exec(Files.Classes.Global.FolderMy.ToString(), report.FileName + ".csv", result.Length, memStream, true);

                    fileURL  = CommonLinkUtility.GetFileWebEditorUrl((int)file.ID);
                    fileURL += string.Format("&options={{\"delimiter\":{0},\"codePage\":{1}}}",
                                             (int)Global.ReportCsvDelimiter.Key,
                                             Encoding.UTF8.CodePage);
                }

                Response.Redirect(fileURL);

                break;
            }
        }
Esempio n. 7
0
        private static String SaveCSVFileInMyDocument(String title, String data)
        {
            string fileURL;

            using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(data)))
            {
                var file = FileUploader.Exec(Files.Classes.Global.FolderMy.ToString(), title, data.Length, memStream, true);

                fileURL = FilesLinkUtility.GetFileWebEditorUrl((int)file.ID);
            }
            fileURL += string.Format("&options={{\"delimiter\":{0},\"codePage\":{1}}}",
                                     (int)FileUtility.CsvDelimiter.Comma,
                                     Encoding.UTF8.CodePage);

            return(fileURL);
        }
Esempio n. 8
0
 private static FileWrapper SaveFile(string folderid, Stream file, string fileName, bool createNewIfExist)
 {
     try
     {
         var resultFile = FileUploader.Exec(folderid, fileName, file.Length, file, createNewIfExist);
         return(new FileWrapper(resultFile));
     }
     catch (FileNotFoundException e)
     {
         throw new ItemNotFoundException("File not found", e);
     }
     catch (DirectoryNotFoundException e)
     {
         throw new ItemNotFoundException("Folder not found", e);
     }
 }
 public FileWrapper <T> InsertFile(T folderId, Stream file, string title, bool?createNewIfExist, bool keepConvertStatus = false)
 {
     try
     {
         var resultFile = FileUploader.Exec(folderId, title, file.Length, file, createNewIfExist ?? !FilesSettingsHelper.UpdateIfExist, !keepConvertStatus);
         return(FileWrapperHelper.Get(resultFile));
     }
     catch (FileNotFoundException e)
     {
         throw new ItemNotFoundException("File not found", e);
     }
     catch (DirectoryNotFoundException e)
     {
         throw new ItemNotFoundException("Folder not found", e);
     }
 }
        internal void SaveDocbuilderReport(ReportState state, string url)
        {
            var data = new System.Net.WebClient().DownloadData(url);

            using (var memStream = new MemoryStream(data))
            {
                Action <Stream> action = stream =>
                {
                    var file = FileUploader.Exec(ProjectsCommonSettings.LoadForCurrentUser().FolderId.ToString(), state.FileName, stream.Length, stream, true);
                    state.FileId = (int)file.ID;
                };

                try
                {
                    action(memStream);
                }
                catch (DirectoryNotFoundException)
                {
                    var settings = ProjectsCommonSettings.LoadForCurrentUser();
                    settings.FolderId = Web.Files.Classes.Global.FolderMy;
                    settings.SaveForCurrentUser();

                    action(memStream);
                }
            }

            using (var scope = DIHelper.Resolve())
            {
                scope.Resolve <EngineFactory>().ReportEngine.Save(new ReportFile
                {
                    FileId     = state.FileId,
                    Name       = Name,
                    ReportType = ReportType
                });
            }
        }
Esempio n. 11
0
        public override FileUploadResult ProcessUpload(HttpContext context)
        {
            var log = LogManager.GetLogger("ASC.Mail.FilesUploader");

            string message;

            var fileName = string.Empty;

            MailAttachmentData mailAttachmentData = null;

            try
            {
                if (!FileToUpload.HasFilesToUpload(context))
                {
                    throw new Exception(MailScriptResource.AttachmentsBadInputParamsError);
                }

                if (!SecurityContext.IsAuthenticated)
                {
                    throw new HttpException(403, "Access denied.");
                }

                Thread.CurrentThread.CurrentCulture   = CurrentCulture;
                Thread.CurrentThread.CurrentUICulture = CurrentCulture;

                var mailId         = Convert.ToInt32(context.Request["messageId"]);
                var copyToMy       = Convert.ToInt32(context.Request["copyToMy"]);
                var needSaveToTemp = Convert.ToBoolean(context.Request["needSaveToTemp"]);

                if (mailId < 1)
                {
                    throw new AttachmentsException(AttachmentsException.Types.MessageNotFound,
                                                   "Message not yet saved!");
                }

                var engine = new EngineFactory(TenantId, Username);

                var item = engine.MessageEngine.GetMessage(mailId, new MailMessageData.Options());
                if (item == null)
                {
                    throw new AttachmentsException(AttachmentsException.Types.MessageNotFound, "Message not found.");
                }

                if (string.IsNullOrEmpty(item.StreamId))
                {
                    throw new AttachmentsException(AttachmentsException.Types.BadParams, "Have no stream");
                }

                var postedFile = new FileToUpload(context);

                fileName = context.Request["name"];

                if (string.IsNullOrEmpty(fileName))
                {
                    throw new AttachmentsException(AttachmentsException.Types.BadParams, "Empty name param");
                }

                if (copyToMy == 1)
                {
                    var uploadedFile = FileUploader.Exec(Global.FolderMy.ToString(), fileName,
                                                         postedFile.ContentLength, postedFile.InputStream, true);

                    return(new FileUploadResult
                    {
                        Success = true,
                        FileName = uploadedFile.Title,
                        FileURL = FileShareLink.GetLink(uploadedFile, false),
                        Data = new MailAttachmentData
                        {
                            fileId = Convert.ToInt32(uploadedFile.ID),
                            fileName = uploadedFile.Title,
                            size = uploadedFile.ContentLength,
                            contentType = uploadedFile.ConvertedType,
                            attachedAsLink = true,
                            tenant = TenantId,
                            user = Username
                        }
                    });
                }

                mailAttachmentData = engine.AttachmentEngine
                                     .AttachFileToDraft(TenantId, Username, mailId, fileName, postedFile.InputStream,
                                                        postedFile.ContentLength, null, postedFile.NeedSaveToTemp);

                return(new FileUploadResult
                {
                    Success = true,
                    FileName = mailAttachmentData.fileName,
                    FileURL = mailAttachmentData.storedFileUrl,
                    Data = mailAttachmentData
                });
            }
            catch (HttpException he)
            {
                log.Error("FileUpload handler failed", he);

                context.Response.StatusCode = he.GetHttpCode();
                message = he.Message != null
                    ? HttpUtility.HtmlEncode(he.Message)
                    : MailApiErrorsResource.ErrorInternalServer;
            }
            catch (AttachmentsException ex)
            {
                log.Error("FileUpload handler failed", ex);

                switch (ex.ErrorType)
                {
                case AttachmentsException.Types.BadParams:
                    message = MailScriptResource.AttachmentsBadInputParamsError;
                    break;

                case AttachmentsException.Types.EmptyFile:
                    message = MailScriptResource.AttachmentsEmptyFileNotSupportedError;
                    break;

                case AttachmentsException.Types.MessageNotFound:
                    message = MailScriptResource.AttachmentsMessageNotFoundError;
                    break;

                case AttachmentsException.Types.TotalSizeExceeded:
                    message = MailScriptResource.AttachmentsTotalLimitError;
                    break;

                case AttachmentsException.Types.DocumentNotFound:
                    message = MailScriptResource.AttachmentsDocumentNotFoundError;
                    break;

                case AttachmentsException.Types.DocumentAccessDenied:
                    message = MailScriptResource.AttachmentsDocumentAccessDeniedError;
                    break;

                default:
                    message = MailScriptResource.AttachmentsUnknownError;
                    break;
                }
            }
            catch (TenantQuotaException ex)
            {
                log.Error("FileUpload handler failed", ex);
                message = ex.Message;
            }
            catch (Exception ex)
            {
                log.Error("FileUpload handler failed", ex);
                message = MailScriptResource.AttachmentsUnknownError;
            }

            return(new FileUploadResult
            {
                Success = false,
                FileName = fileName,
                Data = mailAttachmentData,
                Message = string.IsNullOrEmpty(message) ? MailApiErrorsResource.ErrorInternalServer : message
            });
        }
        private static FileWrapper SaveFile(object folderid, Stream file, string fileName)
        {
            var resultFile = FileUploader.Exec(folderid.ToString(), fileName, file.Length, file);

            return(new FileWrapper(resultFile));
        }
Esempio n. 13
0
        public override FileUploadResult ProcessUpload(HttpContext context)
        {
            var            fileName   = string.Empty;
            MailAttachment attachment = null;

            try
            {
                if (FileToUpload.HasFilesToUpload(context))
                {
                    try
                    {
                        Thread.CurrentThread.CurrentCulture   = CurrentCulture;
                        Thread.CurrentThread.CurrentUICulture = CurrentCulture;

                        var mailId   = Convert.ToInt32(context.Request["messageId"]);
                        var copyToMy = Convert.ToInt32(context.Request["copyToMy"]);

                        if (mailId < 1)
                        {
                            throw new AttachmentsException(AttachmentsException.Types.MessageNotFound, "Message not yet saved!");
                        }

                        var item = MailBoxManager.GetMailInfo(TenantId, Username, mailId, false, false);
                        if (item == null)
                        {
                            throw new AttachmentsException(AttachmentsException.Types.MessageNotFound, "Message not found.");
                        }

                        if (string.IsNullOrEmpty(item.StreamId))
                        {
                            throw new AttachmentsException(AttachmentsException.Types.BadParams, "Have no stream");
                        }

                        var postedFile = new FileToUpload(context);
                        fileName = context.Request["name"];

                        if (copyToMy == 1)
                        {
                            var uploadedFile = FileUploader.Exec(Global.FolderMy.ToString(), fileName, postedFile.ContentLength, postedFile.InputStream, true);
                            return(new FileUploadResult
                            {
                                Success = true,
                                FileName = uploadedFile.Title,
                                FileURL = FilesLinkUtility.GetFileWebPreviewUrl(uploadedFile.Title, uploadedFile.ID),
                                Data = new MailAttachment
                                {
                                    fileId = Convert.ToInt32(uploadedFile.ID),
                                    fileName = uploadedFile.Title,
                                    size = uploadedFile.ContentLength,
                                    contentType = uploadedFile.ConvertedType,
                                    attachedAsLink = true,
                                    tenant = TenantId,
                                    user = Username
                                }
                            });
                        }

                        attachment = new MailAttachment
                        {
                            fileId   = -1,
                            size     = postedFile.ContentLength,
                            fileName = fileName,
                            streamId = item.StreamId,
                            tenant   = TenantId,
                            user     = Username
                        };

                        attachment = MailBoxManager.AttachFile(TenantId, Username, mailId, fileName, postedFile.InputStream);

                        return(new FileUploadResult
                        {
                            Success = true,
                            FileName = attachment.fileName,
                            FileURL = attachment.storedFileUrl,
                            Data = attachment
                        });
                    }
                    catch (AttachmentsException e)
                    {
                        string errorMessage;

                        switch (e.ErrorType)
                        {
                        case AttachmentsException.Types.BadParams:
                            errorMessage = MailScriptResource.AttachmentsBadInputParamsError;
                            break;

                        case AttachmentsException.Types.EmptyFile:
                            errorMessage = MailScriptResource.AttachmentsEmptyFileNotSupportedError;
                            break;

                        case AttachmentsException.Types.MessageNotFound:
                            errorMessage = MailScriptResource.AttachmentsMessageNotFoundError;
                            break;

                        case AttachmentsException.Types.TotalSizeExceeded:
                            errorMessage = MailScriptResource.AttachmentsTotalLimitError;
                            break;

                        case AttachmentsException.Types.DocumentNotFound:
                            errorMessage = MailScriptResource.AttachmentsDocumentNotFoundError;
                            break;

                        case AttachmentsException.Types.DocumentAccessDenied:
                            errorMessage = MailScriptResource.AttachmentsDocumentAccessDeniedError;
                            break;

                        default:
                            errorMessage = MailScriptResource.AttachmentsUnknownError;
                            break;
                        }
                        throw new Exception(errorMessage);
                    }
                    catch (TenantQuotaException)
                    {
                        throw;
                    }
                    catch (Exception)
                    {
                        throw new Exception(MailScriptResource.AttachmentsUnknownError);
                    }
                }
                throw new Exception(MailScriptResource.AttachmentsBadInputParamsError);
            }
            catch (Exception ex)
            {
                return(new FileUploadResult
                {
                    Success = false,
                    FileName = fileName,
                    Data = attachment,
                    Message = ex.Message,
                });
            }
        }
Esempio n. 14
0
        private static FileWrapper SaveFile(string folderid, Stream file, string fileName, bool createNewIfExist)
        {
            var resultFile = FileUploader.Exec(folderid, fileName, file.Length, file, createNewIfExist);

            return(new FileWrapper(resultFile));
        }
        private FileWrapper <int> SaveFile(int folderid, Stream file, string fileName)
        {
            var resultFile = _fileUploader.Exec <int>(folderid, fileName, file.Length, file);

            return(_fileWrapperHelper.Get <int>(resultFile));
        }