Exemplo n.º 1
0
        public FileObject GetFile(string fileId, bool compress = false, string encryptPassword = null)
        {
            FileObject getFile()
            {
                var filePath = _getFilePath(fileId);

                if (!FileUtils.IsFileExists(filePath))
                {
                    return(new FileObject(Stream.Null, fileId));
                }

                var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.Read, 4096, true);

                try
                {
                    if (string.IsNullOrEmpty(encryptPassword))
                    {
                        if (compress)
                        {
                            using (fileStream)
                            {
                                return(new FileObject(FileUtils.DecompressFile(fileStream), fileId));
                            }
                        }
                        else
                        {
                            return(new FileObject(fileStream, fileId));
                        }
                    }
                    else
                    {
                        using (fileStream)
                        {
                            var decryptFile = FileUtils.FileDecrypt(fileStream, encryptPassword);

                            if (compress)
                            {
                                using (decryptFile)
                                {
                                    return(new FileObject(FileUtils.DecompressFile(decryptFile), fileId));
                                }
                            }
                            else
                            {
                                return(new FileObject(decryptFile, fileId));
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    return(new FileObject(Stream.Null, fileId));
                }
            }

            return(_fileManagerOptions.Impersonation.IsImpersonationEnabled ? Impersonation.RunAsUser(_userCredentialsImpersonation, getFile, LogonType.Interactive, LogonProvider.Default) : getFile());
        }
Exemplo n.º 2
0
        public bool CheckFileExists(string fileId)
        {
            bool checkFileExists()
            {
                var filePath = _getFilePath(fileId);

                return(File.Exists(filePath));
            }

            return(_fileManagerOptions.Impersonation.IsImpersonationEnabled ? Impersonation.RunAsUser(_userCredentialsImpersonation, checkFileExists, LogonType.Interactive, LogonProvider.Default) : checkFileExists());
        }
Exemplo n.º 3
0
        public T StorageOperation <T>(Func <T> func)
        {
            if (user.IsNotNullOrEmpty())
            {
                return(Impersonation.RunAsUser(
                           new UserCredentials(domain, user, password),
                           LogonType.NetworkCleartext,
                           func));
            }

            return(func.Invoke());
        }
Exemplo n.º 4
0
        public static bool SendEmail(string emailTo, string emailCc, string emailBcc, string emailSubject, string emailBody, string[] emailFileAttachments, string emailFrom, string emailPassword, string domain, string smtpHost, int smtpPort, bool smtpTLSEnabled)
        {
            var credentials = new UserCredentials(domain, emailFrom, emailPassword);

            Impersonation.RunAsUser(credentials, LogonType.NewCredentials, () =>
            {
                SmtpClient client = new SmtpClient();
                client.Port       = smtpPort;
                client.Host       = smtpHost;
                client.EnableSsl  = smtpTLSEnabled;

                try
                {
                    MailMessage mmsg = new MailMessage();
                    mmsg.From        = new MailAddress(emailFrom);
                    if (!string.IsNullOrEmpty(emailTo))
                    {
                        mmsg.To.Add(emailTo);
                    }
                    if (!string.IsNullOrEmpty(emailCc))
                    {
                        mmsg.CC.Add(emailCc);
                    }
                    if (!string.IsNullOrEmpty(emailBcc))
                    {
                        mmsg.Bcc.Add(emailBcc);
                    }
                    mmsg.Body            = emailBody;
                    mmsg.BodyEncoding    = Encoding.UTF8;
                    mmsg.IsBodyHtml      = true;
                    mmsg.Subject         = emailSubject;
                    mmsg.SubjectEncoding = Encoding.UTF8;

                    if (emailFileAttachments != null)
                    {
                        foreach (var attachment in emailFileAttachments)
                        {
                            mmsg.Attachments.Add(new Attachment(attachment));
                        }
                    }
                    client.Send(mmsg);
                    mmsg.Dispose();
                    client.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Check the Configuration.json file for incorrect SMTP settings, change them and restart the program and click the Send Test Email button." + Environment.NewLine + Environment.NewLine + "Exception: " + Environment.NewLine + ex.Message);
                }
            });
            return(true);
        }
Exemplo n.º 5
0
        public bool SaveFile(string fileId, Stream stream, bool compress = false, string encryptPassword = null)
        {
            bool saveFile()
            {
                try
                {
                    var directoryPath = _getDirectoryPath(fileId);

                    if (compress)
                    {
                        using (var compressedStream = FileUtils.CompressFile(stream))
                        {
                            if (string.IsNullOrEmpty(encryptPassword))
                            {
                                FileUtils.SaveFile(directoryPath, fileId, compressedStream);
                            }
                            else
                            {
                                using (var encryptStream = FileUtils.FileEncrypt(compressedStream, encryptPassword))
                                {
                                    FileUtils.SaveFile(directoryPath, fileId, encryptStream);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(encryptPassword))
                        {
                            FileUtils.SaveFile(directoryPath, fileId, stream);
                        }
                        else
                        {
                            using (var encryptStream = FileUtils.FileEncrypt(stream, encryptPassword))
                            {
                                FileUtils.SaveFile(directoryPath, fileId, encryptStream);
                            }
                        }
                    }

                    return(true);
                }
                catch (Exception e)
                {
                    return(false);
                }
            }

            return(_fileManagerOptions.Impersonation.IsImpersonationEnabled ? Impersonation.RunAsUser(_userCredentialsImpersonation, saveFile, LogonType.Interactive, LogonProvider.Default) : saveFile());
        }
Exemplo n.º 6
0
 public void StorageOperation(Action action)
 {
     if (user.IsNotNullOrEmpty())
     {
         Impersonation.RunAsUser(
             new UserCredentials(domain, user, password),
             LogonType.NetworkCleartext,
             action);
     }
     else
     {
         action.Invoke();
     }
 }
Exemplo n.º 7
0
        public void DeleteFile(string fileId)
        {
            void deleteFile()
            {
                RetryUtils.Do(() => FileUtils.DeleteDirectory(_getDirectoryPath(fileId)), TimeSpan.FromSeconds(1));
            }

            if (_fileManagerOptions.Impersonation.IsImpersonationEnabled)
            {
                Impersonation.RunAsUser(_userCredentialsImpersonation, deleteFile);
            }
            else
            {
                deleteFile();
            }
        }
Exemplo n.º 8
0
        public bool TryDeleteFile(string fileId)
        {
            bool tryDeleteFile()
            {
                try
                {
                    DeleteFile(fileId);
                    return(true);
                }
                catch (Exception e)
                {
                    // log
                    return(false);
                }
            }

            return(_fileManagerOptions.Impersonation.IsImpersonationEnabled ? Impersonation.RunAsUser(_userCredentialsImpersonation, tryDeleteFile, LogonType.Interactive, LogonProvider.Default) : tryDeleteFile());
        }