public override FileUploadResult ProcessUpload(HttpContext context) { if (!SecurityContext.IsAuthenticated) { throw new HttpException(403, "Access denied."); } var result = new FileUploadResult { Success = false }; try { if (FileToUpload.HasFilesToUpload(context)) { var file = new FileToUpload(context); var maxFileSize = MaxFileSizeInMegabytes * 1024 * 1024; if (string.IsNullOrEmpty(file.FileName)) { throw new ArgumentException("Empty file name"); } if (maxFileSize < file.ContentLength) { throw new Exception(CalendarJSResource.calendarEventAttachments_fileSizeError); } var fileName = System.IO.Path.GetFileName(file.FileName); var document = new File { Title = fileName, FolderID = AttachmentEngine.GetTmpFolderId(), ContentLength = file.ContentLength, ThumbnailStatus = Thumbnail.NotRequired }; document = AttachmentEngine.SaveFile(document, file.InputStream); result.Data = new { id = document.ID.ToString(), title = document.Title, size = document.ContentLength, contentType = file.FileContentType, fileUrl = FileShareLink.GetLink(document) + "&tmp=true" }; result.Success = true; } } catch (Exception ex) { result.Success = false; result.Message = ex.Message; } return(result); }
private void InitScript() { var fileId = Request[FilesLinkUtility.FileId]; File file; try { using (var fileDao = Global.DaoFactory.GetFileDao()) { file = fileDao.GetFile(fileId); } } catch (Exception ex) { Global.Logger.Error("ShareLink", ex); Response.StatusCode = (int)HttpStatusCode.BadRequest; return; } if (file == null) { Response.StatusCode = (int)HttpStatusCode.NotFound; return; } if (!FileSharing.CanSetAccess(file)) { Response.StatusCode = (int)HttpStatusCode.Forbidden; return; } var shareRecord = Global.GetFilesSecurity().GetShares(file).FirstOrDefault(r => r.Subject == FileConstant.ShareLinkId); if (shareRecord == null) { Response.StatusCode = (int)HttpStatusCode.Forbidden; return; } Link = FileShareLink.GetLink(file); if (BitlyLoginProvider.Enabled) { try { Link = BitlyLoginProvider.GetShortenLink(Link); } catch (Exception ex) { Global.Logger.Error("Get shorten link", ex); } } }
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 }); }
public static string GetUriString(File file) { return(FileShareLink.GetLink(file)); }