private static void Convert(HttpContext context) { context.Response.ContentType = "text/plain"; try { var fileName = Path.GetFileName(context.Request["filename"]); var fileUri = DocManagerHelper.GetFileUri(fileName, true); var extension = (Path.GetExtension(fileUri) ?? "").Trim('.'); var internalExtension = DocManagerHelper.GetInternalExtension(FileUtility.GetFileType(fileName)).Trim('.'); if (DocManagerHelper.ConvertExts.Contains("." + extension) && !string.IsNullOrEmpty(internalExtension)) { var key = ServiceConverter.GenerateRevisionId(fileUri); string newFileUri; var result = ServiceConverter.GetConvertedUri(fileUri, extension, internalExtension, key, true, out newFileUri); if (result != 100) { context.Response.Write("{ \"step\" : \"" + result + "\", \"filename\" : \"" + fileName + "\"}"); return; } var correctName = DocManagerHelper.GetCorrectName(Path.GetFileNameWithoutExtension(fileName) + "." + internalExtension); var req = (HttpWebRequest)WebRequest.Create(newFileUri); using (var stream = req.GetResponse().GetResponseStream()) { if (stream == null) { throw new Exception("Stream is null"); } const int bufferSize = 4096; using (var fs = File.Open(DocManagerHelper.StoragePath(correctName), FileMode.Create)) { var buffer = new byte[bufferSize]; int readed; while ((readed = stream.Read(buffer, 0, bufferSize)) != 0) { fs.Write(buffer, 0, readed); } } } Remove(fileName); fileName = correctName; DocManagerHelper.CreateMeta(fileName, context.Request.Cookies.GetOrDefault("uid", ""), context.Request.Cookies.GetOrDefault("uname", "")); } context.Response.Write("{ \"filename\" : \"" + fileName + "\"}"); } catch (Exception e) { context.Response.Write("{ \"error\": \"" + e.Message + "\"}"); } }
public ActionResult Sample(string fileExt, bool?sample) { var fileName = DocManagerHelper.CreateDemo(fileExt, sample ?? false); DocManagerHelper.CreateMeta(fileName, Request.Cookies.GetOrDefault("uid", ""), Request.Cookies.GetOrDefault("uname", "")); Response.Redirect(Url.Action("Editor", "Home", new { fileName = fileName })); return(null); }
// creating a sample document public ActionResult Sample(string fileExt, bool?sample) { var fileName = DocManagerHelper.CreateDemo(fileExt, sample ?? false); // create a sample document var id = Request.Cookies.GetOrDefault("uid", null); var user = Users.getUser(id); DocManagerHelper.CreateMeta(fileName, user.id, user.name); // create meta information for the sample document Response.Redirect(Url.Action("Editor", "Home", new { fileName = fileName })); return(null); }
// upload a file private static void Upload(HttpContext context) { context.Response.ContentType = "text/plain"; try { DocManagerHelper.VerifySSL(); var httpPostedFile = context.Request.Files[0]; string fileName; // check from which browser the request came for if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE") { var files = httpPostedFile.FileName.Split(new char[] { '\\' }); fileName = files[files.Length - 1]; // get file name } else { fileName = httpPostedFile.FileName; } var curSize = httpPostedFile.ContentLength; if (DocManagerHelper.MaxFileSize < curSize || curSize <= 0) // check if the file size exceeds the maximum file size { throw new Exception("File size is incorrect"); } var curExt = (Path.GetExtension(fileName) ?? "").ToLower(); if (!DocManagerHelper.FileExts.Contains(curExt)) // check if the file extension is supported by the editor { throw new Exception("File type is not supported"); } fileName = DocManagerHelper.GetCorrectName(fileName); // get the correct file name if such a name already exists var documentType = FileUtility.GetFileType(fileName).ToString().ToLower(); var savedFileName = DocManagerHelper.StoragePath(fileName); // get the storage path to the uploading file httpPostedFile.SaveAs(savedFileName); // and save it // get file meta information or create the default one var id = context.Request.Cookies.GetOrDefault("uid", null); var user = Users.getUser(id); DocManagerHelper.CreateMeta(fileName, user.id, user.name); context.Response.Write("{ \"filename\": \"" + fileName + "\", \"documentType\": \"" + documentType + "\"}"); } catch (Exception e) { context.Response.Write("{ \"error\": \"" + e.Message + "\"}"); } }
private static void Upload(HttpContext context) { context.Response.ContentType = "text/plain"; try { var httpPostedFile = context.Request.Files[0]; string fileName; if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE") { var files = httpPostedFile.FileName.Split(new char[] { '\\' }); fileName = files[files.Length - 1]; } else { fileName = httpPostedFile.FileName; } var curSize = httpPostedFile.ContentLength; if (DocManagerHelper.MaxFileSize < curSize || curSize <= 0) { throw new Exception("File size is incorrect"); } var curExt = (Path.GetExtension(fileName) ?? "").ToLower(); if (!DocManagerHelper.FileExts.Contains(curExt)) { throw new Exception("File type is not supported"); } fileName = DocManagerHelper.GetCorrectName(fileName); var savedFileName = DocManagerHelper.StoragePath(fileName); httpPostedFile.SaveAs(savedFileName); DocManagerHelper.CreateMeta(fileName, context.Request.Cookies.GetOrDefault("uid", ""), context.Request.Cookies.GetOrDefault("uname", "")); context.Response.Write("{ \"filename\": \"" + fileName + "\"}"); } catch (Exception e) { context.Response.Write("{ \"error\": \"" + e.Message + "\"}"); } }
private static void SaveAs(HttpContext context) { context.Response.ContentType = "text/plain"; try { string fileData; try { using (var receiveStream = context.Request.InputStream) using (var readStream = new StreamReader(receiveStream)) { fileData = readStream.ReadToEnd(); if (string.IsNullOrEmpty(fileData)) { context.Response.Write("{\"error\":\"Request stream is empty\"}"); } } } catch (Exception e) { throw new HttpException((int)HttpStatusCode.BadRequest, e.Message); } var jss = new JavaScriptSerializer(); var body = jss.Deserialize <Dictionary <string, object> >(fileData); var fileUrl = (string)body["url"]; var title = (string)body["title"]; var fileName = DocManagerHelper.GetCorrectName(title); var extension = "." + (Path.GetExtension(fileName).ToLower() ?? "").Trim('.'); var allExt = DocManagerHelper.ConvertExts .Concat(DocManagerHelper.EditedExts) .Concat(DocManagerHelper.ViewedExts) .Concat(DocManagerHelper.FillFormExts) .ToArray(); if (!allExt.Contains(extension)) { context.Response.Write("{\"error\":\"File type is not supported\"}"); } var req = (HttpWebRequest)WebRequest.Create(fileUrl); DocManagerHelper.VerifySSL(); using (var stream = req.GetResponse().GetResponseStream()) { if (stream == null || req.GetResponse().ContentLength <= 0 || req.GetResponse().ContentLength > DocManagerHelper.MaxFileSize) { context.Response.Write("{\"error\": \"File size is incorrect\"}"); } const int bufferSize = 4096; using (var fs = File.Open(DocManagerHelper.StoragePath(fileName, null), FileMode.Create)) { var buffer = new byte[bufferSize]; int readed; while ((readed = stream.Read(buffer, 0, bufferSize)) != 0) { fs.Write(buffer, 0, readed); // write bytes to the output stream } } } var id = context.Request.Cookies.GetOrDefault("uid", null); var user = Users.getUser(id); // get the user DocManagerHelper.CreateMeta(fileName, user.id, user.name, null); context.Response.Write("{ \"file\": \"" + fileName + "\"}"); } catch (Exception e) { context.Response.Write("{ \"error\": \"" + 1 + "\", \"message\": \"" + e.Message + "\"}"); } }
// convert a file private static void Convert(HttpContext context) { context.Response.ContentType = "text/plain"; try { string fileData; using (var receiveStream = context.Request.InputStream) using (var readStream = new StreamReader(receiveStream)) { fileData = readStream.ReadToEnd(); if (string.IsNullOrEmpty(fileData)) { context.Response.Write("{\"error\":1,\"message\":\"Request stream is empty\"}"); } } var jss = new JavaScriptSerializer(); var body = jss.Deserialize <Dictionary <string, object> >(fileData); var fileName = Path.GetFileName(body["filename"].ToString()); var lang = context.Request.Cookies.GetOrDefault("ulang", null); var filePass = body["filePass"] != null ? body["filePass"].ToString() : null; var fileUri = DocManagerHelper.GetDownloadUrl(fileName); var extension = (Path.GetExtension(fileName).ToLower() ?? "").Trim('.'); var internalExtension = DocManagerHelper.GetInternalExtension(FileUtility.GetFileType(fileName)).Trim('.'); // check if the file with such an extension can be converted if (DocManagerHelper.ConvertExts.Contains("." + extension) && !string.IsNullOrEmpty(internalExtension)) { // generate document key var key = ServiceConverter.GenerateRevisionId(fileUri); var downloadUri = new UriBuilder(DocManagerHelper.GetServerUrl(true)) { Path = HttpRuntime.AppDomainAppVirtualPath + (HttpRuntime.AppDomainAppVirtualPath.EndsWith("/") ? "" : "/") + "webeditor.ashx", Query = "type=download&fileName=" + HttpUtility.UrlEncode(fileName) }; // get the url to the converted file string newFileUri; var result = ServiceConverter.GetConvertedUri(downloadUri.ToString(), extension, internalExtension, key, true, out newFileUri, filePass, lang); if (result != 100) { context.Response.Write("{ \"step\" : \"" + result + "\", \"filename\" : \"" + fileName + "\"}"); return; } // get a file name of an internal file extension with an index if the file with such a name already exists var correctName = DocManagerHelper.GetCorrectName(Path.GetFileNameWithoutExtension(fileName) + "." + internalExtension); var req = (HttpWebRequest)WebRequest.Create(newFileUri); DocManagerHelper.VerifySSL(); using (var stream = req.GetResponse().GetResponseStream()) // get response stream of the converting file { if (stream == null) { throw new Exception("Stream is null"); } const int bufferSize = 4096; using (var fs = File.Open(DocManagerHelper.StoragePath(correctName), FileMode.Create)) { var buffer = new byte[bufferSize]; int readed; while ((readed = stream.Read(buffer, 0, bufferSize)) != 0) { fs.Write(buffer, 0, readed); // write bytes to the output stream } } } Remove(fileName); // remove the original file and its history if it exists fileName = correctName; // create meta information about the converted file with user id and name specified var id = context.Request.Cookies.GetOrDefault("uid", null); var user = Users.getUser(id); DocManagerHelper.CreateMeta(fileName, user.id, user.name); } var documentType = FileUtility.GetFileType(fileName).ToString().ToLower(); context.Response.Write("{ \"filename\" : \"" + fileName + "\", \"documentType\": \"" + documentType + "\" }"); } catch (Exception e) { context.Response.Write("{ \"error\": \"" + e.Message + "\"}"); } }