Exemplo n.º 1
0
        /// <summary>
        /// Index Page
        /// </summary>
        /// <returns>Index Page</returns>
        public IActionResult Index()
        {
            string nlbUrls = FileLockHelper.GetNLBUrls(_hostingEnvironment.ContentRootPath);

            ViewData["NLBMsg"] = string.IsNullOrEmpty(nlbUrls) ? "未配置" : "已配置";
            return(View());
        }
        public ActionResult <string> SetNLBLock()
        {
            try
            {
                string postString = string.Empty;
                using (StreamReader sr = new StreamReader(Request.Body, Encoding.UTF8))
                {
                    postString = sr.ReadToEndAsync().Result;
                }

                if (!string.IsNullOrEmpty(postString))
                {
                    var postLock = JsonConvert.DeserializeObject <PostLock>(postString);

                    FileLockHelper.SetLock(postLock.FileId, postLock.FileLock, null);
                    return("Set Success.");
                }
                else
                {
                    log.Info($"SetNLBLock Exception: Post Data Is Null.");
                    return("Post Data Is Null.");
                }
            }
            catch (Exception ex)
            {
                log.Info($"SetNLBLock Exception: {ex.Message}");
                return(ex.Message);
            }
        }
Exemplo n.º 3
0
        public IActionResult WriteNLBUrls(string urls)
        {
            log.Info($"WriteNLBUrls:{urls}");
            try
            {
                if (string.IsNullOrEmpty(urls))
                {
                    return(Json(new JsonModel()
                    {
                        Success = false, Message = "请输入NLB 其他网址!"
                    }));
                }

                var    urlList  = urls.Split(",");
                string errorMsg = string.Empty;
                foreach (string url in urlList)
                {
                    try
                    {
                        string nlbUrl = url;
                        if (nlbUrl.EndsWith("/"))
                        {
                            nlbUrl = nlbUrl.TrimEnd('/');
                        }

                        HttpClient webRequest = FileLockHelper.GetHttpClient();
                        var        result     = webRequest.GetStringAsync($"{nlbUrl}/NLBLock/GetNLBPoint").GetAwaiter().GetResult();
                    }
                    catch
                    {
                        errorMsg += url + ",";
                    }
                }
                if (!string.IsNullOrEmpty(errorMsg))
                {
                    return(Json(new JsonModel()
                    {
                        Success = false, Message = $"网址: {errorMsg} 无法访问, 请重新输入!"
                    }));
                }

                FileLockHelper.SetNLBUrls(_hostingEnvironment.ContentRootPath, urls);
                log.Info($"WriteNLBUrls Set NLBUrls:{urls}");
                return(Json(new JsonModel()
                {
                    Success = true, Message = "修改成功!"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new JsonModel()
                {
                    Success = false, Message = ex.Message
                }));
            }
        }
        public IActionResult ProcessLock(string id)
        {
            log.Info($"ProcessLock file id: {id}, WopiOverrideHeader: {WopiOverrideHeader}");

            string oldLock = Request.Headers[WopiHeaders.OldLock];
            string newLock = Request.Headers[WopiHeaders.Lock];

            lock (FileLockHelper.LockStorage)
            {
                bool lockAcquired = FileLockHelper.TryGetLock(id, out var existingLock);
                log.Info($"ProcessLock lockAcquired: {lockAcquired}, oldLock: {oldLock}, newLock: {newLock}");
                switch (WopiOverrideHeader)
                {
                case "GET_LOCK":
                    if (lockAcquired)
                    {
                        Response.Headers[WopiHeaders.Lock] = existingLock.Lock;
                    }
                    return(new OkResult());

                case "LOCK":
                case "PUT":
                    if (string.IsNullOrEmpty(oldLock))
                    {
                        // Lock / put
                        if (lockAcquired)
                        {
                            log.Info($"ProcessLock existingLock: {existingLock.Lock}");
                            if (existingLock.Lock == newLock)
                            {
                                // File is currently locked and the lock ids match, refresh lock
                                FileLockHelper.SetLock(id, newLock, _hostingEnvironment.ContentRootPath);

                                return(new OkResult());
                            }
                            else
                            {
                                // There is a valid existing lock on the file
                                return(ReturnLockMismatch(Response, existingLock.Lock));
                            }
                        }
                        else
                        {
                            // The file is not currently locked, create and store new lock information
                            FileLockHelper.SetLock(id, newLock, _hostingEnvironment.ContentRootPath);

                            return(new OkResult());
                        }
                    }
                    else
                    {
                        // Unlock and relock (http://wopi.readthedocs.io/projects/wopirest/en/latest/files/UnlockAndRelock.html)
                        if (lockAcquired)
                        {
                            log.Info($"ProcessLock existingLock: {existingLock.Lock}");
                            if (existingLock.Lock == oldLock)
                            {
                                // Replace the existing lock with the new one
                                FileLockHelper.SetLock(id, newLock, _hostingEnvironment.ContentRootPath);

                                return(new OkResult());
                            }
                            else
                            {
                                // The existing lock doesn't match the requested one. Return a lock mismatch error along with the current lock
                                return(ReturnLockMismatch(Response, existingLock.Lock));
                            }
                        }
                        else
                        {
                            // The requested lock does not exist which should result in a lock mismatch error.
                            return(ReturnLockMismatch(Response, reason: "File not locked"));
                        }
                    }

                case "UNLOCK":
                    if (lockAcquired)
                    {
                        if (existingLock.Lock == newLock)
                        {
                            // Remove valid lock
                            FileLockHelper.SetLock(id, null, _hostingEnvironment.ContentRootPath);

                            log.Info($"ProcessLock UNLOCK: OkResult");
                            return(new OkResult());
                        }
                        else
                        {
                            // The existing lock doesn't match the requested one. Return a lock mismatch error along with the current lock
                            return(ReturnLockMismatch(Response, existingLock.Lock));
                        }
                    }
                    else
                    {
                        // The requested lock does not exist.
                        return(ReturnLockMismatch(Response, reason: "File not locked"));
                    }

                case "REFRESH_LOCK":
                    if (lockAcquired)
                    {
                        if (existingLock.Lock == newLock)
                        {
                            // Extend the lock timeout
                            FileLockHelper.SetLock(id, newLock, _hostingEnvironment.ContentRootPath);

                            return(new OkResult());
                        }
                        else
                        {
                            // The existing lock doesn't match the requested one. Return a lock mismatch error along with the current lock
                            return(ReturnLockMismatch(Response, existingLock.Lock));
                        }
                    }
                    else
                    {
                        // The requested lock does not exist.  That's also a lock mismatch error.
                        return(ReturnLockMismatch(Response, reason: "File not locked"));
                    }
                }
            }
            return(new OkResult());
        }