/// <summary>
        /// Download a document chunk
        /// </summary>
        /// <param name="logonId"></param>
        /// <param name="TransferId">Transfer id obtained when starting the download</param>
        /// <param name="FilePosition">Position in the file to start downloading the chunk</param>
        /// <returns></returns>
        public DownloadChunkReturnValue DocumentDownloadChunk(Guid logonId, Guid TransferId, long FilePosition)
        {
            DownloadChunkReturnValue ReturnValue = new DownloadChunkReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    FileChunkData FileChunkData = FileTransfer.DownloadChunk(logonId, TransferId, FilePosition);

                    ReturnValue.ChunkSize = FileChunkData.ChunkSize;
                    ReturnValue.Bytes = FileChunkData.Bytes;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (Exception ex)
            {
                ReturnValue.Success = false;
                ReturnValue.Message = ex.Message;
            }

            return ReturnValue;
        }
Esempio n. 2
0
        /// <summary>
        /// Download a document chunk
        /// </summary>
        /// <param name="logonId"></param>
        /// <param name="TransferId">Transfer id obtained when starting the download</param>
        /// <param name="FilePosition">Position in the file to start downloading the chunk</param>
        /// <returns></returns>
        public DownloadChunkReturnValue DocumentDownloadChunk(Guid logonId, Guid TransferId, long FilePosition)
        {
            DownloadChunkReturnValue ReturnValue = new DownloadChunkReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    FileChunkData FileChunkData = FileTransfer.DownloadChunk(logonId, TransferId, FilePosition);

                    ReturnValue.ChunkSize = FileChunkData.ChunkSize;
                    ReturnValue.Bytes     = FileChunkData.Bytes;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (Exception ex)
            {
                ReturnValue.Success = false;
                ReturnValue.Message = ex.Message;
            }

            return(ReturnValue);
        }
        /// <summary>
        /// Download a document chunk
        /// </summary>
        /// <param name="logonId"></param>
        /// <param name="TransferId">Transfer id obtained when starting the download</param>
        /// <param name="FilePosition">Position in the file to start downloading the chunk</param>
        /// <returns></returns>
        public DownloadChunkReturnValue DocumentDownloadChunk(Guid logonId, Guid TransferId, long FilePosition)
        {
            DownloadChunkReturnValue ReturnValue = new DownloadChunkReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            // Can do everything
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    FileChunkData FileChunkData = FileTransfer.DownloadChunk(logonId, TransferId, FilePosition);

                    ReturnValue.ChunkSize = FileChunkData.ChunkSize;
                    ReturnValue.Bytes = FileChunkData.Bytes;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                ReturnValue.Success = false;
                ReturnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                ReturnValue.Success = false;
                ReturnValue.Message = ex.Message;
            }

            return ReturnValue;
        }
        /// <summary>
        /// Download a document chunk
        /// </summary>
        /// <param name="oHostSecurityToken"></param>
        /// <param name="TransferId">Transfer id obtained when starting the download</param>
        /// <param name="FilePosition">Position in the file to start downloading the chunk</param>
        /// <returns></returns>
        public DownloadChunkReturnValue DocumentDownloadChunk(HostSecurityToken oHostSecurityToken, Guid TransferId, long FilePosition)
        {
            DownloadChunkReturnValue ReturnValue = null;

            if (Functions.ValidateIWSToken(oHostSecurityToken))
            {
                oDocumentService = new DocumentService();
                ReturnValue      = oDocumentService.DocumentDownloadChunk(Functions.GetLogonIdFromToken(oHostSecurityToken), TransferId, FilePosition);
            }
            else
            {
                ReturnValue         = new DownloadChunkReturnValue();
                ReturnValue.Success = false;
                ReturnValue.Message = "Invalid Token";
            }
            return(ReturnValue);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void _grdDocFiles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "select")
            {
                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                if (row.Cells[0].FindControl("_linkEdit") != null)
                {
                    int rowId = ((GridViewRow)((LinkButton)(e.CommandSource)).NamingContainer).RowIndex;
                    int docId = Convert.ToInt32(_grdDocFiles.DataKeys[rowId].Values["Id"].ToString());

                    Session[SessionName.DocumentId] = docId;
                    Response.Redirect("~/DocumentManagement/ImportDocument.aspx", true);
                }
            }
            else if (e.CommandName == "reupload")
            {
                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                if (row.Cells[0].FindControl("_linkReupload") != null)
                {
                    int rowId = ((GridViewRow)((LinkButton)(e.CommandSource)).NamingContainer).RowIndex;
                    int docId = Convert.ToInt32(_grdDocFiles.DataKeys[rowId].Values["Id"].ToString());

                    Session[SessionName.DocumentId] = docId;
                    Response.Redirect("~/DocumentManagement/ReuploadDocument.aspx", true);
                }
            }
            else if (e.CommandName == "fileDownload")
            {
                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                if (row.Cells[0].FindControl("_linkFileDownload") != null)
                {
                    int rowId = ((GridViewRow)((LinkButton)(e.CommandSource)).NamingContainer).RowIndex;
                    int docId = Convert.ToInt32(_grdDocFiles.DataKeys[rowId].Values["Id"].ToString());

                    DocumentServiceClient _documentServiceClient = new DocumentServiceClient();
                    try
                    {
                        Guid ProjectId = DataConstants.DummyGuid;
                        StartDocumentDownloadReturnValue Header;
                        if (!string.IsNullOrEmpty(Convert.ToString(Session[SessionName.ProjectId])))
                        {
                            ProjectId = new Guid(Convert.ToString(Session[SessionName.ProjectId]));
                        }

                        #region Start Download
                        try
                        {
                            Header = _documentServiceClient.StartDocumentDownloadForMatter(((LogonReturnValue)Session[SessionName.LogonSettings]).LogonId, ProjectId, docId);
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        #endregion

                        if (Header != null)
                        {
                            if (!Header.Success)
                            {
                                _lblError.CssClass = "errorMessage";
                                _lblError.Text     = Header.Message;
                                return;
                            }

                            if (!Directory.Exists(Path.Combine(Server.MapPath("."), "Download")))
                            {
                                Directory.CreateDirectory(Path.Combine(Server.MapPath("."), "Download"));
                            }

                            long   BytesLeft          = Header.Size;
                            string downloadFolderPath = Path.Combine(Server.MapPath("."), "Download");
                            string FileName           = Path.Combine(downloadFolderPath, Header.FileName);

                            try
                            {
                                #region Download Chunk by Chunk
                                // Open the file
                                using (FileStream FileStream = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.Read))
                                {
                                    while (BytesLeft > 0)
                                    {
                                        // Download a chunk
                                        DownloadChunkReturnValue Chunk = _documentServiceClient.DocumentDownloadChunk(
                                            ((LogonReturnValue)Session[SessionName.LogonSettings]).LogonId, Header.TransferId, FileStream.Position);

                                        if (!Chunk.Success)
                                        {
                                            throw new Exception("File download failed: " + Chunk.Message);
                                        }

                                        // Write the chunk to the file
                                        FileStream.Write(Chunk.Bytes, 0, Chunk.ChunkSize);

                                        BytesLeft -= Chunk.ChunkSize;
                                    }
                                }
                                #endregion

                                #region Download Complete

                                // Tell the service we have finished downloading
                                _documentServiceClient.DocumentDownloadComplete(((LogonReturnValue)Session[SessionName.LogonSettings]).LogonId, Header.TransferId);
                                #endregion

                                // Check the file size
                                if (new FileInfo(FileName).Length != Header.Size)
                                {
                                    throw new Exception("File download failed, file size is wrong");
                                }

                                // Make a hash of the file
                                byte[] LocalHash = FileTransferHash.CreateFileMD5Hash(FileName);

                                // Check the hash matches that from the server
                                if (!FileTransferHash.CheckHash(LocalHash, Header.Hash))
                                {
                                    throw new Exception("File download failed, document checksum does not match");
                                }

                                // Set the file modified date
                                File.SetLastWriteTime(FileName, Header.ModifiedDate);

                                try
                                {
                                    //string jscript = "HideProgress();window.open('DocumentView.aspx?file={0}&path={1}','FileDownload','height=200,width=400,status=no,toolbar=no,menubar=no,left=0,top=0');";
                                    string jscript = "HideProgress(); GetDocument('" + AppFunctions.GetASBaseUrl() + "/Pages/DocMgmt/DocumentView.aspx?file={0}&path={1}');";
                                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(),
                                                                        Guid.NewGuid().ToString(),
                                                                        string.Format(jscript, HttpUtility.UrlEncode(new FileInfo(FileName).Name), HttpUtility.UrlEncode(FileName)),
                                                                        true);
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }
                            }
                            catch
                            {
                                // Download failed so delete file being downloaded
                                File.Delete(FileName);
                                throw;
                            }
                        }
                    }
                    catch (System.ServiceModel.EndpointNotFoundException)
                    {
                        _lblError.Text     = DataConstants.WSEndPointErrorMessage;
                        _lblError.CssClass = "errorMessage";
                    }
                    catch (Exception ex)
                    {
                        _lblError.CssClass = "errorMessage";
                        _lblError.Text     = ex.Message;
                    }
                    finally
                    {
                        if (_documentServiceClient != null)
                        {
                            if (_documentServiceClient.State != System.ServiceModel.CommunicationState.Faulted)
                            {
                                _documentServiceClient.Close();
                            }
                        }
                    }
                }
            }
        }