private static void SaveFile(HttpContext context) { try { var shareLink = context.Request[UrlConstant.DocUrlKey] ?? ""; var fileID = context.Request[UrlConstant.FileId]; if (string.IsNullOrEmpty(fileID)) { throw new ArgumentNullException(fileID); } var downloadUri = context.Request[UrlConstant.FileUri]; if (string.IsNullOrEmpty(downloadUri)) { throw new ArgumentNullException(downloadUri); } using (var fileDao = Global.DaoFactory.GetFileDao()) { File file; var checkLink = DocumentUtils.CheckShareLink(shareLink, false, fileDao, out file); if (!checkLink && file == null) { file = fileDao.GetFile(fileID); } if (file == null) { throw new HttpException((int)HttpStatusCode.NotFound, FilesCommonResource.ErrorMassage_FileNotFound); } if (!checkLink && !Global.GetFilesSecurity().CanEdit(file)) { throw new HttpException((int)HttpStatusCode.Forbidden, FilesCommonResource.ErrorMassage_SecurityException); } if (file.RootFolderType == FolderType.TRASH) { throw new HttpException((int)HttpStatusCode.Forbidden, FilesCommonResource.ErrorMassage_ViewTrashItem); } FileLocker.Add(file.ID); var versionEdit = context.Request[UrlConstant.Version]; var currentType = file.ConvertedType ?? FileUtility.GetFileExtension(file.Title); var newType = FileUtility.GetFileExtension(downloadUri); var updateVersion = file.Version > 1 || file.ConvertedType == null || string.IsNullOrEmpty(context.Request[UrlConstant.New]); if ((string.IsNullOrEmpty(versionEdit) || file.Version <= Convert.ToInt32(versionEdit) || currentType != newType) && updateVersion) { file.Version++; } else { updateVersion = false; fileDao.DeleteFileStream(file.ID); } file.ConvertedType = newType; if (string.IsNullOrEmpty(file.ProviderName)) { var bytes = new WebClient().DownloadData(downloadUri); file.ContentLength = bytes.Length; using (var stream = new MemoryStream(bytes)) { file = fileDao.SaveFile(file, stream); } } else { //TODO: service must convert with outputType param var fileExt = FileUtility.GetFileExtension(file.Title); //???HACK for google & msdoc if (file.ProviderName == nSupportedCloudConfigurations.Google.ToString() && fileExt.Equals(".doc")) { fileExt = "docx"; } using (var readStream = DocumentUtils.GetConvertedFile(file, fileExt, downloadUri).GetBuffered()) { if (readStream != null) { file.ContentLength = readStream.Length; file = fileDao.SaveFile(file, readStream); } } } if (!updateVersion) { return; } Global.PublishUpdateDocument(file.ID); } } catch (Exception e) { context.Response.Write("{ \"error\": \"true\", \"message\": \"" + e.Message + "\"}"); } }
private static void DownloadFile(HttpContext context, bool inline) { var id = context.Request[UrlConstant.FileId]; var ver = context.Request[UrlConstant.Version]; var shareLink = context.Request[UrlConstant.DocUrlKey] ?? ""; var outType = context.Request[UrlConstant.OutType]; using (var fileDao = Global.DaoFactory.GetFileDao()) { File file; using (var tagDao = Global.DaoFactory.GetTagDao()) { var checkLink = DocumentUtils.CheckShareLink(shareLink, true, fileDao, out file); if (!checkLink && file == null) { file = String.IsNullOrEmpty(ver) ? fileDao.GetFile(id) : fileDao.GetFile(id, Convert.ToInt32(ver)); } if (file == null) { throw new HttpException((int)HttpStatusCode.NotFound, FilesCommonResource.ErrorMassage_FileNotFound); } 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)) { context.Response.Redirect((context.Request.UrlReferrer != null ? context.Request.UrlReferrer.ToString() : PathProvider.StartURL) + "#" + UrlConstant.Error + "/" + HttpUtility.UrlEncode(FilesCommonResource.ErrorMassage_FileNotFound)); return; } tagDao.RemoveTags(Tag.New(SecurityContext.CurrentAccount.ID, file)); } context.Response.Clear(); context.Response.ContentType = file.ContentType; context.Response.Charset = "utf-8"; var browser = context.Request.Browser.Browser; if (browser == "AppleMAC-Safari" && 0 <= context.Request.UserAgent.IndexOf("chrome", StringComparison.InvariantCultureIgnoreCase)) { browser = "Chrome"; } var format = browser == "IE" || browser == "AppleMAC-Safari" ? "{0}; filename=\"{1}\"" : "{0}; filename*=utf-8''{1}"; var title = file.Title.Replace(',', '_'); var filename = browser == "AppleMAC-Safari" ? title : HttpUtility.UrlPathEncode(title); var contentDisposition = string.Format(format, inline ? "inline" : "attachment", filename); context.Response.AddHeader("Content-Disposition", contentDisposition); if (inline && string.Equals(context.Request.Headers["If-None-Match"], GetEtag(file))) { //Its cached. Reply 304 context.Response.StatusCode = 304; context.Response.Cache.SetETag(GetEtag(file)); } else { context.Response.CacheControl = "public"; context.Response.Cache.SetETag(GetEtag(file)); context.Response.Cache.SetCacheability(HttpCacheability.Public); if (file.ConvertedType == null && (string.IsNullOrEmpty(outType) || inline)) { //NOTE: always pass files through handler using (var readStream = fileDao.GetFileStream(file)) { context.Response.AddHeader("Content-Length", readStream.Length.ToString()); //BUG:Can be bugs readStream.StreamCopyTo(context.Response.OutputStream); } } else { var ext = FileUtility.GetFileExtension(file.Title).Trim('.'); if (!string.IsNullOrEmpty(outType) && !inline) { outType = outType.Trim('.'); if (FileUtility.ExtConvertible[ext].Contains(outType)) { ext = outType; } } //Take from converter using (var readStream = DocumentUtils.GetConvertedFile(file, ext)) { if (readStream != null) { readStream.StreamCopyTo(context.Response.OutputStream); } } } try { context.Response.Flush(); context.Response.End(); } catch (HttpException) { } } } }