public ActionResult Preview(
            int id
            , string entityName
            , string userToken
            )
        {
            UserInfo userInfo = GetUserInfoFromUserToken(userToken);

            try
            {
                if (userInfo != null)
                {
                    if (entityName.ToLower() == "document")
                    {
                        List <DocumentAttachment> docAttachmentList = new List <DocumentAttachment>();

                        using (VistosDbContext ctx = new VistosDbContext())
                        {
                            docAttachmentList = ctx.DocumentAttachment.Where(d => !d.Deleted && d.Document_FK == id).ToList();
                        }

                        if (docAttachmentList.Count == 1)
                        {
                            DocumentAttachment att = docAttachmentList.Single();

                            if (att.StoreInFtp)
                            {
                                FtpService ftpService = new FtpService(
                                    Settings.GetInstance.SystemSettings.FtpType
                                    , Settings.GetInstance.SystemSettings.FtpHost
                                    , Settings.GetInstance.SystemSettings.FtpPort
                                    , Settings.GetInstance.SystemSettings.FtpUserName
                                    , Settings.GetInstance.SystemSettings.FtpPassword
                                    , Settings.GetInstance.SystemSettings.FtpPrivateKey
                                    , Settings.GetInstance.SystemSettings.FtpPassPhrase
                                    , Settings.GetInstance.SystemSettings.FtpRoot
                                    );
                                byte[] content = ftpService.DownloadDocAttachment(att.Id, att.DocName);

                                MemoryStream dataStream = new MemoryStream();
                                dataStream.Write(content, 0, content.Length);
                                dataStream.Position = 0;
                                return(new FileStreamResult(dataStream, att.ContentType));
                            }
                            else if (att.StoreInDropBox)
                            {
                                DropBoxService dropBox          = new DropBoxService(Settings.GetInstance.SystemSettings.DropBoxSecurityToken);
                                Uri            dropBoxAttachUri = dropBox.GetUriDocAttachment(att.Id, att.DocName);
                                return(new RedirectResult(dropBoxAttachUri.ToString(), false));
                            }
                            else
                            {
                                MemoryStream dataStream = new MemoryStream();
                                dataStream.Write(att.Attachment, 0, att.Attachment.Length);
                                dataStream.Position = 0;
                                return(new FileStreamResult(dataStream, att.ContentType));
                            }
                        }
                    }
                }
                return(NotFound());
            }
            catch (Exception ex)
            {
                return(NotFound());

                throw;
            }
        }
        public ActionResult Download(
            int id
            , string entityName
            , string userToken
            )
        {
            UserInfo userInfo = GetUserInfoFromUserToken(userToken);

            try
            {
                if (userInfo != null)
                {
                    if (entityName.ToLower() == "document")
                    {
                        List <DocumentAttachment> docAttachmentList = new List <DocumentAttachment>();

                        using (VistosDbContext ctx = new VistosDbContext())
                        {
                            docAttachmentList = ctx.DocumentAttachment.Where(d => !d.Deleted && d.Document_FK == id).ToList();
                        }

                        if (docAttachmentList.Count == 1)
                        {
                            DocumentAttachment att = docAttachmentList.Single();

                            if (att.StoreInFtp)
                            {
                                FtpService ftpService = new FtpService(
                                    Settings.GetInstance.SystemSettings.FtpType
                                    , Settings.GetInstance.SystemSettings.FtpHost
                                    , Settings.GetInstance.SystemSettings.FtpPort
                                    , Settings.GetInstance.SystemSettings.FtpUserName
                                    , Settings.GetInstance.SystemSettings.FtpPassword
                                    , Settings.GetInstance.SystemSettings.FtpPrivateKey
                                    , Settings.GetInstance.SystemSettings.FtpPassPhrase
                                    , Settings.GetInstance.SystemSettings.FtpRoot
                                    );
                                byte[] content = ftpService.DownloadDocAttachment(att.Id, att.DocName);
                                return(File(content, att.ContentType, att.DocName));
                            }
                            else if (att.StoreInDropBox)
                            {
                                DropBoxService dropBox          = new DropBoxService(Settings.GetInstance.SystemSettings.DropBoxSecurityToken);
                                Uri            dropBoxAttachUri = dropBox.GetUriDocAttachment(att.Id, att.DocName);
                                return(new RedirectResult(dropBoxAttachUri.ToString(), false));
                            }
                            else
                            {
                                return(File(att.Attachment, att.ContentType, att.DocName));
                            }
                        }
                        else
                        {
                            using (MemoryStream str = new MemoryStream())
                            {
                                using (ZipArchive zipFile = new ZipArchive(str, ZipArchiveMode.Update, false))
                                {
                                    foreach (DocumentAttachment att in docAttachmentList)
                                    {
                                        byte[] content;

                                        if (att.StoreInFtp)
                                        {
                                            FtpService ftpService = new FtpService(
                                                Settings.GetInstance.SystemSettings.FtpType
                                                , Settings.GetInstance.SystemSettings.FtpHost
                                                , Settings.GetInstance.SystemSettings.FtpPort
                                                , Settings.GetInstance.SystemSettings.FtpUserName
                                                , Settings.GetInstance.SystemSettings.FtpPassword
                                                , Settings.GetInstance.SystemSettings.FtpPrivateKey
                                                , Settings.GetInstance.SystemSettings.FtpPassPhrase
                                                , Settings.GetInstance.SystemSettings.FtpRoot
                                                );
                                            content = ftpService.DownloadDocAttachment(att.Id, att.DocName);
                                        }
                                        else if (att.StoreInDropBox)
                                        {
                                            DropBoxService dropBox = new DropBoxService(Settings.GetInstance.SystemSettings.DropBoxSecurityToken);
                                            content = dropBox.DownloadDocAttachment(att.Id, att.DocName);
                                        }
                                        else
                                        {
                                            content = att.Attachment;
                                        }

                                        if (content != null)
                                        {
                                            ZipArchiveEntry zipEntry = zipFile.CreateEntry(att.DocName);
                                            using (MemoryStream originalFileStream = new MemoryStream(content))
                                            {
                                                using (Stream zipEntryStream = zipEntry.Open())
                                                {
                                                    originalFileStream.CopyTo(zipEntryStream);
                                                }
                                            }
                                        }
                                    }
                                }
                                return(File(str.ToArray(), "application/octet-stream", "Download.zip"));
                            }
                        }
                    }
                }
                return(NotFound());
            }
            catch (Exception ex)
            {
                return(NotFound());

                throw;
            }
        }