Beispiel #1
0
        private void ProcessEdit <T>(T fileId, TrackerData fileData)
        {
            if (ThirdPartySelector.GetAppByFileId(fileId.ToString()) != null)
            {
                return;
            }

            var users     = FileTracker.GetEditingBy(fileId);
            var usersDrop = new List <string>();

            string docKey;
            var    app = ThirdPartySelector.GetAppByFileId(fileId.ToString());

            if (app == null)
            {
                File <T> fileStable;
                fileStable = DaoFactory.GetFileDao <T>().GetFileStable(fileId);

                docKey = DocumentServiceHelper.GetDocKey(fileStable);
            }
            else
            {
                docKey = fileData.Key;
            }

            if (!fileData.Key.Equals(docKey))
            {
                Logger.InfoFormat("DocService editing file {0} ({1}) with key {2} for {3}", fileId, docKey, fileData.Key, string.Join(", ", fileData.Users));
                usersDrop = fileData.Users;
            }
            else
            {
                foreach (var user in fileData.Users)
                {
                    if (!Guid.TryParse(user, out var userId))
                    {
                        Logger.Info("DocService userId is not Guid: " + user);
                        continue;
                    }
                    users.Remove(userId);

                    try
                    {
                        var doc = FileShareLink.CreateKey(fileId);
                        EntryManager.TrackEditing(fileId, userId, userId, doc);
                    }
                    catch (Exception e)
                    {
                        Logger.DebugFormat("Drop command: fileId '{0}' docKey '{1}' for user {2} : {3}", fileId, fileData.Key, user, e.Message);
                        usersDrop.Add(userId.ToString());
                    }
                }
            }

            if (usersDrop.Any())
            {
                if (!DocumentServiceHelper.DropUser(fileData.Key, usersDrop.ToArray(), fileId))
                {
                    Logger.Error("DocService drop failed for users " + string.Join(",", usersDrop));
                }
            }

            foreach (var removeUserId in users)
            {
                FileTracker.Remove(fileId, userId: removeUserId);
            }
            SocketManager.FilesChangeEditors(fileId);
        }
Beispiel #2
0
        private TrackResponse ProcessSave <T>(T fileId, TrackerData fileData)
        {
            var comments = new List <string>();

            if (fileData.Status == TrackerStatus.Corrupted ||
                fileData.Status == TrackerStatus.CorruptedForceSave)
            {
                comments.Add(FilesCommonResource.ErrorMassage_SaveCorrupted);
            }

            var forcesave = fileData.Status == TrackerStatus.ForceSave || fileData.Status == TrackerStatus.CorruptedForceSave;

            if (fileData.Users == null || fileData.Users.Count == 0 || !Guid.TryParse(fileData.Users[0], out var userId))
            {
                userId = Guid.Empty;
            }

            var app = ThirdPartySelector.GetAppByFileId(fileId.ToString());

            if (app == null)
            {
                File <T> fileStable;
                fileStable = DaoFactory.GetFileDao <T>().GetFileStable(fileId);

                var docKey = DocumentServiceHelper.GetDocKey(fileStable);
                if (!fileData.Key.Equals(docKey))
                {
                    Logger.ErrorFormat("DocService saving file {0} ({1}) with key {2}", fileId, docKey, fileData.Key);

                    StoringFileAfterError(fileId, userId.ToString(), DocumentServiceConnector.ReplaceDocumentAdress(fileData.Url));
                    return(new TrackResponse {
                        Message = "Expected key " + docKey
                    });
                }
            }

            UserInfo user = null;

            try
            {
                SecurityContext.AuthenticateMeWithoutCookie(userId);

                user = UserManager.GetUsers(userId);
                var culture = string.IsNullOrEmpty(user.CultureName) ? TenantManager.GetCurrentTenant().GetCulture() : CultureInfo.GetCultureInfo(user.CultureName);
                Thread.CurrentThread.CurrentCulture   = culture;
                Thread.CurrentThread.CurrentUICulture = culture;
            }
            catch (Exception ex)
            {
                Logger.Info("DocService save error: anonymous author - " + userId, ex);
                if (!userId.Equals(ASC.Core.Configuration.Constants.Guest.ID))
                {
                    comments.Add(FilesCommonResource.ErrorMassage_SaveAnonymous);
                }
            }

            File <T> file        = null;
            var      saveMessage = "Not saved";

            if (string.IsNullOrEmpty(fileData.Url))
            {
                try
                {
                    comments.Add(FilesCommonResource.ErrorMassage_SaveUrlLost);

                    file = EntryManager.CompleteVersionFile(fileId, 0, false, false);

                    DaoFactory.GetFileDao <T>().UpdateComment(file.ID, file.Version, string.Join("; ", comments));

                    file = null;
                    Logger.ErrorFormat("DocService save error. Empty url. File id: '{0}'. UserId: {1}. DocKey '{2}'", fileId, userId, fileData.Key);
                }
                catch (Exception ex)
                {
                    Logger.Error(string.Format("DocService save error. Version update. File id: '{0}'. UserId: {1}. DocKey '{2}'", fileId, userId, fileData.Key), ex);
                }
            }
            else
            {
                if (fileData.Encrypted)
                {
                    comments.Add(FilesCommonResource.CommentEditEncrypt);
                }

                var forcesaveType = ForcesaveType.None;
                if (forcesave)
                {
                    switch (fileData.ForceSaveType)
                    {
                    case TrackerData.ForceSaveInitiator.Command:
                        forcesaveType = ForcesaveType.Command;
                        break;

                    case TrackerData.ForceSaveInitiator.Timer:
                        forcesaveType = ForcesaveType.Timer;
                        break;

                    case TrackerData.ForceSaveInitiator.User:
                        forcesaveType = ForcesaveType.User;
                        break;
                    }
                    comments.Add(fileData.ForceSaveType == TrackerData.ForceSaveInitiator.User
                                     ? FilesCommonResource.CommentForcesave
                                     : FilesCommonResource.CommentAutosave);
                }

                try
                {
                    file        = EntryManager.SaveEditing(fileId, null, DocumentServiceConnector.ReplaceDocumentAdress(fileData.Url), null, string.Empty, string.Join("; ", comments), false, fileData.Encrypted, forcesaveType, true);
                    saveMessage = fileData.Status == TrackerStatus.MustSave || fileData.Status == TrackerStatus.ForceSave ? null : "Status " + fileData.Status;
                }
                catch (Exception ex)
                {
                    Logger.Error(string.Format("DocService save error. File id: '{0}'. UserId: {1}. DocKey '{2}'. DownloadUri: {3}", fileId, userId, fileData.Key, fileData.Url), ex);
                    saveMessage = ex.Message;

                    StoringFileAfterError(fileId, userId.ToString(), DocumentServiceConnector.ReplaceDocumentAdress(fileData.Url));
                }
            }

            if (!forcesave)
            {
                FileTracker.Remove(fileId);
            }

            if (file != null)
            {
                if (user != null)
                {
                    FilesMessageService.Send(file, MessageInitiator.DocsService, MessageAction.UserFileUpdated, user.DisplayUserName(false, DisplayUserSettingsHelper), file.Title);
                }

                if (!forcesave)
                {
                    SaveHistory(file, (fileData.History ?? "").ToString(), DocumentServiceConnector.ReplaceDocumentAdress(fileData.ChangesUrl));
                }
            }

            SocketManager.FilesChangeEditors(fileId, !forcesave);

            var result = new TrackResponse {
                Message = saveMessage
            };

            return(result);
        }
        private static string ProcessSave(string fileId, TrackerData fileData)
        {
            Guid userId;
            var  comments = new List <string>();

            if (fileData.Status == TrackerStatus.Corrupted)
            {
                comments.Add(FilesCommonResource.ErrorMassage_SaveCorrupted);
            }

            if (fileData.Users == null || fileData.Users.Count == 0 || !Guid.TryParse(fileData.Users[0], out userId))
            {
                userId = FileTracker.GetEditingBy(fileId).FirstOrDefault();
            }

            File file;
            var  app = ThirdPartySelector.GetAppByFileId(fileId);

            if (app == null)
            {
                using (var fileDao = Global.DaoFactory.GetFileDao())
                {
                    file = fileDao.GetFile(fileId);
                }

                var docKey = DocumentServiceHelper.GetDocKey(file);
                if (!fileData.Key.Equals(docKey))
                {
                    Global.Logger.ErrorFormat("DocService saving file {0} ({1}) with key {2}", fileId, docKey, fileData.Key);

                    StoringFileAfterError(fileId, userId.ToString(), DocumentServiceConnector.ReplaceDocumentAdress(fileData.Url));
                    return("0");
                }
            }

            try
            {
                SecurityContext.AuthenticateMe(userId);
            }
            catch (Exception ex)
            {
                Global.Logger.Info("DocService save error: anonymous author - " + userId, ex);
                if (!userId.Equals(ASC.Core.Configuration.Constants.Guest.ID))
                {
                    comments.Add(FilesCommonResource.ErrorMassage_SaveAnonymous);
                }
            }

            file = null;
            var saved = false;

            if (string.IsNullOrEmpty(fileData.Url))
            {
                try
                {
                    comments.Add(FilesCommonResource.ErrorMassage_SaveUrlLost);

                    file = EntryManager.CompleteVersionFile(fileId, 0, false, false);

                    using (var fileDao = Global.DaoFactory.GetFileDao())
                    {
                        fileDao.UpdateComment(file.ID, file.Version, string.Join("; ", comments));
                    }

                    file = null;
                    Global.Logger.ErrorFormat("DocService save error. Empty url. File id: '{0}'. UserId: {1}. DocKey '{2}'", fileId, userId, fileData.Key);
                }
                catch (Exception ex)
                {
                    Global.Logger.Error(string.Format("DocService save error. Version update. File id: '{0}'. UserId: {1}. DocKey '{2}'", fileId, userId, fileData.Key), ex);
                }
            }
            else
            {
                if (fileData.Encrypted)
                {
                    comments.Add(FilesCommonResource.CommentEditEncrypt);
                }

                try
                {
                    file  = EntryManager.SaveEditing(fileId, null, DocumentServiceConnector.ReplaceDocumentAdress(fileData.Url), null, string.Empty, string.Join("; ", comments), false, fileData.Encrypted);
                    saved = fileData.Status == TrackerStatus.MustSave;
                }
                catch (Exception ex)
                {
                    Global.Logger.Error(string.Format("DocService save error. File id: '{0}'. UserId: {1}. DocKey '{2}'. DownloadUri: {3}", fileId, userId, fileData.Key, fileData.Url), ex);

                    StoringFileAfterError(fileId, userId.ToString(), DocumentServiceConnector.ReplaceDocumentAdress(fileData.Url));
                }
            }

            FileTracker.Remove(fileId);

            if (file != null)
            {
                var user = CoreContext.UserManager.GetUsers(userId);
                if (user != null)
                {
                    FilesMessageService.Send(file, MessageInitiator.DocsService, MessageAction.UserFileUpdated, user.DisplayUserName(false), file.Title);
                }

                SaveHistory(file, (fileData.History ?? "").ToString(), DocumentServiceConnector.ReplaceDocumentAdress(fileData.ChangesUrl));
            }

            Global.SocketManager.FilesChangeEditors(fileId, true);

            return(string.Format("{{\"error\":{0}{1}}}",
                                 saved
                                     ? "0"  //error:0 - saved
                                     : "1", //error:1 - some error
                                 saved && file != null && file.Encrypted
                                     ? string.Format(",\"addresses\":[{0}]", string.Join(",", BlockchainAddress.GetAddress(file.ID.ToString())))
                                     : string.Empty
                                 ));
        }