public PutRelativeFile Put_Relative(int accessTokenId) { var result = new PutRelativeFile(); var token = _tokenManager.GetToken(accessTokenId); var newFilePath = string.Empty; var target = Request.Headers.Contains("X-WOPI-RelativeTarget") ? Request.Headers.GetValues("X-WOPI-RelativeTarget").First() : Request.Headers.GetValues("X-WOPI-SuggestedTarget").First(); bool overwrite = Request.Headers.Contains("X-WOPI-RelativeTarget") && Convert.ToBoolean(Request.Headers.GetValues("X-WOPI-OverwriteRelativeTarget").First()); if (string.IsNullOrEmpty(target)) { throw new HttpResponseException(HttpStatusCode.BadRequest); } if (target.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Count() > 1) { var fileName = Path.GetFileName(token.FilePath); newFilePath = token.FilePath.ReplaceLast(fileName, target); } else { newFilePath = Path.ChangeExtension(token.FilePath, target); } if (overwrite == false && _webDavManager.FileExist(newFilePath)) { throw new HttpResponseException(HttpStatusCode.Conflict); } var bytes = Request.Content.ReadAsByteArrayAsync().Result; _webDavManager.UploadFile(newFilePath, bytes); var newToken = _tokenManager.CreateToken(WspContext.User, newFilePath); var readUrlPart = Url.Route(FileSystemRouteNames.ViewOfficeOnline, new { org = WspContext.User.OrganizationId, pathPart = newFilePath }); var writeUrlPart = Url.Route(FileSystemRouteNames.EditOfficeOnline, new { org = WspContext.User.OrganizationId, pathPart = newFilePath }); result.HostEditUrl = new Uri(Request.RequestUri, writeUrlPart).ToString(); result.HostViewUrl = new Uri(Request.RequestUri, readUrlPart).ToString();; result.Name = Path.GetFileName(newFilePath); result.Url = Url.GenerateWopiUrl(newToken, newFilePath); return(result); }
public IActionResult PutRelativeFile(string id) { // EnableBuffering Request.EnableBuffering(); string token = Request.Query["access_token"]; log.Info($"PutRelativeFile file id: {id}, access_token: {token}"); // New Name string newName = Utf7ToUtf8(Request.Headers[WopiHeaders.RelativeTarget]); // File Full Path string fileFullPath = string.Empty; // Save new File local path string wholeDirectory = string.Empty; try { log.Info($"PutRelativeFile New File Start: {newName}"); wholeDirectory = $"{_hostingEnvironment.ContentRootPath}/Files/{Guid.NewGuid().ToString()}/"; // Create directory Directory.CreateDirectory(wholeDirectory); // New file to local fileFullPath = wholeDirectory + newName; // Save file to local using (FileStream fs = new FileStream(fileFullPath, FileMode.OpenOrCreate)) { const int bufferLen = 50 * 1024 * 1024; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = Request.Body.Read(buffer, 0, bufferLen)) > 0) { fs.Write(buffer, 0, count); } fs.Dispose(); } log.Info($"PutRelativeFile New File End. File: {fileFullPath}"); } catch (Exception ex) { log.Info($"PutRelativeFile Request Save: {ex.Message}"); } // Upload file to server UploadRes uploadRes = UploadFile(token, fileFullPath, true); // Delete local path RemovePathWithFile(wholeDirectory); // new url for new file var dynamicObj = JsonConvert.DeserializeObject <dynamic>(HttpUtility.UrlDecode(token)); string host = dynamicObj["host"].Value; var port = dynamicObj["port"].Value; if (port > 0) { host = $"{host}:{port}"; } string docId = uploadRes.DocId.Replace("://", "=").Replace("/", "%2F"); // new url string fileUrl = $"{host}/#/preview?{docId}"; string newUrl = $"{Request.Scheme}://{Request.Host.Value}/UnifyEditor.html?Url={HttpUtility.UrlEncode(fileUrl)}"; // Return Result PutRelativeFile putRelativeFile = new PutRelativeFile() { Name = newName, Url = host, HostEditUrl = newUrl, HostViewUrl = newUrl }; string jsonString = JsonConvert.SerializeObject(putRelativeFile); log.Info($"PutRelativeFile Json: {jsonString}"); return(Content(jsonString)); }