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)); fromFile.ID = fromSelector.ConvertId(fromFile.ID); using (var fromFileStream = fromFile.ConvertedType != null ? DocumentUtils.GetConvertedFile(fromFile).GetBuffered() : fromFileDao.GetFileStream(fromFile)) { fromFile.ID = null; //Reset id, so it can be created by apropriate provider fromFile.FolderID = toSelector.ConvertId(toFolderId); var toFile = toFileDao.SaveFile(fromFile, fromFileStream); if (deleteSourceFile) { //Delete source file if needed fromFileDao.DeleteFileStream(fromSelector.ConvertId(fromFileId)); fromFileDao.DeleteFile(fromSelector.ConvertId(fromFileId)); } return(toFile); } }
private Stream CompressToZip(NameValueCollection entries) { var stream = TempStream.Create(); using (var zip = new ZipOutputStream(stream)) { zip.IsStreamOwner = false; zip.SetLevel(3); zip.UseZip64 = UseZip64.Off; foreach (var title in entries.AllKeys) { if (Canceled) { zip.Dispose(); stream.Dispose(); return(null); } var counter = 0; foreach (var path in entries[title].Split(',')) { var newtitle = title; if (0 < counter) { var suffix = " (" + counter + ")"; newtitle = 0 < newtitle.IndexOf('.') ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix) : newtitle + suffix; } var zipentry = new ZipEntry(newtitle) { DateTime = DateTime.UtcNow }; lock (zip) { ZipConstants.DefaultCodePage = Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage; zip.PutNextEntry(zipentry); } if (!string.IsNullOrEmpty(path)) { var file = Global.DaoFactory.GetFileDao().GetFile(path); if (file.ConvertedType != null) { //Take from converter try { using (var readStream = DocumentUtils.GetConvertedFile(file)) { if (readStream != null) { readStream.StreamCopyTo(zip); } } } catch { } } else { using (var readStream = Global.DaoFactory.GetFileDao().GetFileStream(file)) { readStream.StreamCopyTo(zip); } } } counter++; } lock (zip) { ZipConstants.DefaultCodePage = Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage; zip.CloseEntry(); } ProgressStep(); } return(stream); } }
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) { } } } }