internal SmtpService GetSmtpServiceInternal(string name = null)
        {
            EntityRecord smtpServiceRec = null;

            if (name != null)
            {
                var result = new EqlCommand("SELECT * FROM smtp_service WHERE name = @name", new EqlParameter("name", name)).Execute();
                if (result.Count == 0)
                {
                    throw new Exception($"SmtpService with name '{name}' not found.");
                }

                smtpServiceRec = result[0];
            }
            else
            {
                var result = new EqlCommand("SELECT * FROM smtp_service WHERE is_default = @is_default", new EqlParameter("is_default", true)).Execute();
                if (result.Count == 0)
                {
                    throw new Exception($"Default SmtpService not found.");
                }
                else if (result.Count > 1)
                {
                    throw new Exception($"More than one default SmtpService not found.");
                }

                smtpServiceRec = result[0];
            }
            return(smtpServiceRec.MapTo <SmtpService>());
        }
Beispiel #2
0
        public UserFile CreateUserFile(string path = "", string alt = "", string caption = "")
        {
            var userFileRecord = new EntityRecord();

            if (path.StartsWith("/fs"))
            {
                path = path.Substring(3);
            }
            var tempFile = Fs.Find(path);

            if (tempFile == null)
            {
                throw new Exception("File not found on that path");
            }
            var newFileId = Guid.NewGuid();

            userFileRecord["id"]      = newFileId;
            userFileRecord["alt"]     = alt;
            userFileRecord["caption"] = caption;
            var fileKilobytes = Math.Round(((decimal)tempFile.GetBytes().Length / 1024), 2);

            userFileRecord["size"] = fileKilobytes;
            userFileRecord["name"] = Path.GetFileName(path);
            var fileExtension = Path.GetExtension(path);
            var mimeType      = MimeMapping.GetMimeMapping(path);

            if (mimeType.StartsWith("image"))
            {
                var dimensionsRecord = Helpers.GetImageDimension(tempFile.GetBytes());
                userFileRecord["width"]  = (decimal)dimensionsRecord["width"];
                userFileRecord["height"] = (decimal)dimensionsRecord["height"];
                userFileRecord["type"]   = "image";
            }
            else if (mimeType.StartsWith("video"))
            {
                userFileRecord["type"] = "video";
            }
            else if (mimeType.StartsWith("audio"))
            {
                userFileRecord["type"] = "audio";
            }
            else if (fileExtension == ".doc" || fileExtension == ".docx" || fileExtension == ".odt" || fileExtension == ".rtf" ||
                     fileExtension == ".txt" || fileExtension == ".pdf" || fileExtension == ".html" || fileExtension == ".htm" || fileExtension == ".ppt" ||
                     fileExtension == ".pptx" || fileExtension == ".xls" || fileExtension == ".xlsx" || fileExtension == ".ods" || fileExtension == ".odp")
            {
                userFileRecord["type"] = "document";
            }
            else
            {
                userFileRecord["type"] = "other";
            }

            var newFilePath = $"/file/{newFileId}/{Path.GetFileName(path)}";

            using (DbConnection con = DbContext.Current.CreateConnection())
            {
                con.BeginTransaction();
                try
                {
                    var file = Fs.Move(path, newFilePath, false);
                    if (file == null)
                    {
                        throw new Exception("File move from temp folder failed");
                    }

                    userFileRecord["path"] = newFilePath;
                    var response = RecMan.CreateRecord("user_file", userFileRecord);
                    if (!response.Success)
                    {
                        throw new Exception(response.Message);
                    }

                    userFileRecord = response.Object.Data.First();
                    con.CommitTransaction();
                }
                catch (Exception ex)
                {
                    con.RollbackTransaction();
                    throw ex;
                }
            }
            return(userFileRecord.MapTo <UserFile>());
        }