private static void CreateFile(HttpContext context)
        {
            var responseMessage = context.Request["response"] == "message";
            var folderId        = context.Request[FilesLinkUtility.FolderId];

            if (string.IsNullOrEmpty(folderId))
            {
                folderId = Global.FolderMy.ToString();
            }
            Folder folder;

            using (var folderDao = Global.DaoFactory.GetFolderDao())
            {
                folder = folderDao.GetFolder(folderId);
            }
            if (folder == null)
            {
                throw new HttpException((int)HttpStatusCode.NotFound, FilesCommonResource.ErrorMassage_FolderNotFound);
            }
            if (!Global.GetFilesSecurity().CanCreate(folder))
            {
                throw new HttpException((int)HttpStatusCode.Forbidden, FilesCommonResource.ErrorMassage_SecurityException_Create);
            }

            File file;
            var  fileUri   = context.Request[FilesLinkUtility.FileUri];
            var  fileTitle = context.Request[FilesLinkUtility.FileTitle];

            try
            {
                if (!string.IsNullOrEmpty(fileUri))
                {
                    file = CreateFileFromUri(folder, fileUri, fileTitle);
                }
                else
                {
                    var docType = context.Request["doctype"];
                    file = CreateFileFromTemplate(folder, fileTitle, docType);
                }
            }
            catch (Exception ex)
            {
                Global.Logger.Error(ex);
                if (responseMessage)
                {
                    context.Response.Write("error: " + ex.Message);
                    return;
                }
                context.Response.Redirect(PathProvider.StartURL + "#error/" + HttpUtility.UrlEncode(ex.Message), true);
                return;
            }

            FileMarker.MarkAsNew(file);

            if (responseMessage)
            {
                context.Response.Write("ok: " + string.Format(FilesCommonResource.MessageFileCreated, folder.Title));
                return;
            }

            context.Response.Redirect(
                (context.Request["openfolder"] ?? "").Equals("true")
                    ? PathProvider.GetFolderUrl(file.FolderID)
                    : (FilesLinkUtility.GetFileWebEditorUrl(file.ID) + "#message/" + HttpUtility.UrlEncode(string.Format(FilesCommonResource.MessageFileCreated, folder.Title))));
        }
Example #2
0
        private static void SaveFile(HttpContext context)
        {
            try
            {
                var shareLinkKey = context.Request[CommonLinkUtility.DocShareKey] ?? "";

                var fileID = context.Request[CommonLinkUtility.FileId];

                if (string.IsNullOrEmpty(fileID))
                {
                    throw new ArgumentNullException(fileID);
                }

                var downloadUri = context.Request[CommonLinkUtility.FileUri];
                if (string.IsNullOrEmpty(downloadUri))
                {
                    throw new ArgumentNullException(downloadUri);
                }

                using (var fileDao = Global.DaoFactory.GetFileDao())
                {
                    File file;

                    var checkLink = FileShareLink.Check(shareLinkKey, 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) || CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor()))
                    {
                        throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_EditFile);
                    }
                    if (file.RootFolderType == FolderType.TRASH)
                    {
                        throw new HttpException((int)HttpStatusCode.Forbidden, FilesCommonResource.ErrorMassage_ViewTrashItem);
                    }

                    var versionEdit   = context.Request[CommonLinkUtility.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 &&
                        !FileLocker.LockVersion(file.ID))
                    {
                        file.Version++;
                    }

                    file.ConvertedType = newType;

                    if (file.ProviderEntry && !newType.Equals(currentType))
                    {
                        var key = DocumentServiceConnector.GenerateRevisionId(downloadUri);

                        DocumentServiceConnector.GetConvertedUri(downloadUri, newType, currentType, key, false, out downloadUri);
                    }

                    var req = (HttpWebRequest)WebRequest.Create(downloadUri);

                    using (var editedFileStream = new ResponseStream(req.GetResponse()))
                    {
                        file.ContentLength = editedFileStream.Length;

                        file = fileDao.SaveFile(file, editedFileStream);
                    }

                    bool checkRight;
                    var  tabId = new Guid(context.Request["tabId"]);
                    FileLocker.ProlongLock(file.ID, tabId, true, out checkRight);
                    if (checkRight)
                    {
                        FileLocker.ChangeRight(file.ID, SecurityContext.CurrentAccount.ID, false);
                    }

                    FileMarker.MarkAsNew(file);
                    FileMarker.RemoveMarkAsNew(file);
                }
            }
            catch (Exception ex)
            {
                Global.Logger.Error(ex.Message, ex);
                context.Response.Write("{ \"error\": \"true\", \"message\": \"" + ex.Message + "\" }");
            }
        }
Example #3
0
        public static File SaveDocument(string envelopeId, string documentId, string documentName, object folderId)
        {
            if (string.IsNullOrEmpty(envelopeId))
            {
                throw new ArgumentNullException("envelopeId");
            }
            if (string.IsNullOrEmpty(documentId))
            {
                throw new ArgumentNullException("documentId");
            }

            var token         = DocuSignToken.GetToken();
            var account       = GetDocuSignAccount(token);
            var configuration = GetConfiguration(account, token);

            using (var fileDao = Global.DaoFactory.GetFileDao())
                using (var folderDao = Global.DaoFactory.GetFolderDao())
                {
                    if (string.IsNullOrEmpty(documentName))
                    {
                        documentName = "new.pdf";
                    }

                    Folder folder;
                    if (folderId == null ||
                        (folder = folderDao.GetFolder(folderId)) == null ||
                        folder.RootFolderType == FolderType.TRASH ||
                        !Global.GetFilesSecurity().CanCreate(folder))
                    {
                        if (Global.FolderMy != null)
                        {
                            folderId = Global.FolderMy;
                        }
                        else
                        {
                            throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_Create);
                        }
                    }

                    var file = new File
                    {
                        FolderID = folderId,
                        Comment  = FilesCommonResource.CommentCreateByDocuSign,
                        Title    = FileUtility.ReplaceFileExtension(documentName, ".pdf"),
                    };

                    var envelopesApi = new EnvelopesApi(configuration);
                    Log.Info("DocuSign webhook get stream: " + documentId);
                    using (var stream = envelopesApi.GetDocument(account.AccountId, envelopeId, documentId))
                    {
                        file.ContentLength = stream.Length;
                        file = fileDao.SaveFile(file, stream);
                    }

                    FilesMessageService.Send(file, MessageInitiator.ThirdPartyProvider, MessageAction.DocumentSignComplete, "DocuSign", file.Title);

                    FileMarker.MarkAsNew(file);

                    return(file);
                }
        }
        protected override void Do()
        {
            if (_files.Count == 0)
            {
                return;
            }

            var parent = FolderDao.GetFolder(_parentId);

            if (parent == null)
            {
                throw new DirectoryNotFoundException(FilesCommonResource.ErrorMassage_FolderNotFound);
            }
            if (!FilesSecurity.CanCreate(parent))
            {
                throw new System.Security.SecurityException(FilesCommonResource.ErrorMassage_SecurityException_Create);
            }
            if (parent.RootFolderType == FolderType.TRASH)
            {
                throw new Exception(FilesCommonResource.ErrorMassage_ImportToTrash);
            }
            if (parent.ProviderEntry)
            {
                throw new System.Security.SecurityException(FilesCommonResource.ErrorMassage_SecurityException_Create);
            }

            var to =
                FolderDao.GetFolder(_folderName, _parentId)
                ?? FolderDao.SaveFolder(
                    new Folder
            {
                FolderType     = FolderType.DEFAULT,
                ParentFolderID = _parentId,
                Title          = _folderName
            });

            foreach (var f in _files)
            {
                if (Canceled)
                {
                    return;
                }
                try
                {
                    long size;
                    using (var stream = _docProvider.GetDocumentStream(f.ContentLink, out size))
                    {
                        if (stream == null)
                        {
                            throw new Exception("Can not import document " + f.ContentLink + ". Empty stream.");
                        }

                        if (SetupInfo.MaxUploadSize < size)
                        {
                            throw FileSizeComment.FileSizeException;
                        }

                        var folderId = to.ID;
                        var pos      = f.Title.LastIndexOf('/');
                        if (0 < pos)
                        {
                            folderId = GetOrCreateHierarchy(f.Title.Substring(0, pos), to);
                            f.Title  = f.Title.Substring(pos + 1);
                        }

                        f.Title = Global.ReplaceInvalidCharsAndTruncate(f.Title);
                        var file = new File
                        {
                            Title         = f.Title,
                            FolderID      = folderId,
                            ContentLength = size,
                        };

                        var conflict = FileDao.GetFile(file.FolderID, file.Title);
                        if (conflict != null)
                        {
                            if (_overwrite)
                            {
                                if (!FilesSecurity.CanEdit(conflict))
                                {
                                    throw new Exception(FilesCommonResource.ErrorMassage_SecurityException);
                                }
                                if ((conflict.FileStatus & FileStatus.IsEditing) == FileStatus.IsEditing)
                                {
                                    throw new Exception(FilesCommonResource.ErrorMassage_SecurityException_UpdateEditingFile);
                                }
                                if (EntryManager.FileLockedForMe(conflict))
                                {
                                    throw new Exception(FilesCommonResource.ErrorMassage_LockedFile);
                                }

                                file.ID      = conflict.ID;
                                file.Version = conflict.Version + 1;
                            }
                            else
                            {
                                continue;
                            }
                        }

                        if (size <= 0L)
                        {
                            using (var buffered = stream.GetBuffered())
                            {
                                size = buffered.Length;

                                if (SetupInfo.MaxUploadSize < size)
                                {
                                    throw FileSizeComment.FileSizeException;
                                }

                                file.ContentLength = size;
                                try
                                {
                                    file = FileDao.SaveFile(file, buffered);
                                }
                                catch (Exception error)
                                {
                                    FileDao.DeleteFile(file.ID);
                                    throw error;
                                }
                            }
                        }
                        else
                        {
                            try
                            {
                                file = FileDao.SaveFile(file, stream);
                                FilesMessageService.Send(file, httpRequestHeaders, MessageAction.FileImported, parent.Title, file.Title, _docProvider.Name);
                            }
                            catch (Exception error)
                            {
                                FileDao.DeleteFile(file.ID);
                                throw error;
                            }
                        }

                        FileMarker.MarkAsNew(file, _markAsNewRecipientIDs);
                    }
                }
                catch (Exception ex)
                {
                    Error = ex.Message;
                    Logger.Error(Error, ex);
                }
                finally
                {
                    ProgressStep();
                }
            }
        }