| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use uuid::Uuid; |
| 9 | |
| 10 | use crate::constant::{MAX_FILE_CONTENT_TYPE_LEN, MAX_FILE_NAME_LEN}; |
| 11 | use crate::domain::monotonic_id::next_uuid; |
| 12 | use crate::domain::note::NoteId; |
| 13 | use crate::error::ValidationError; |
| 14 | |
| 15 | |
| 16 | |
| 17 | #[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type)] |
| 18 | #[sqlx(transparent)] |
| 19 | pub struct NoteFileId(Uuid); |
| 20 | |
| 21 | impl NoteFileId { |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | pub fn generate() -> Self { |
| 27 | Self(next_uuid()) |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | pub fn uuid(&self) -> Uuid { |
| 33 | self.0 |
| 34 | } |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | pub fn from_key(key: &str) -> Self { |
| 41 | Self(Uuid::parse_str(key).unwrap_or(Uuid::nil())) |
| 42 | } |
| 43 | |
| 44 | |
| 45 | pub fn key(&self) -> String { |
| 46 | self.0.to_string() |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 54 | #[sqlx(transparent)] |
| 55 | pub struct FileName(String); |
| 56 | |
| 57 | impl FileName { |
| 58 | pub fn try_new(value: &str) -> Result<Self, ValidationError> { |
| 59 | let value = value.trim(); |
| 60 | if value.is_empty() { |
| 61 | return Err(ValidationError::Empty("filename")); |
| 62 | } |
| 63 | if value.chars().count() > MAX_FILE_NAME_LEN { |
| 64 | return Err(ValidationError::TooLong { |
| 65 | field: "filename", |
| 66 | max: MAX_FILE_NAME_LEN, |
| 67 | got: value.chars().count(), |
| 68 | }); |
| 69 | } |
| 70 | if value |
| 71 | .chars() |
| 72 | .any(|c| c.is_control() || c == '/' || c == '\\') |
| 73 | { |
| 74 | return Err(ValidationError::Invalid { |
| 75 | field: "filename", |
| 76 | reason: "must not contain path separators or control characters", |
| 77 | }); |
| 78 | } |
| 79 | Ok(Self(value.to_string())) |
| 80 | } |
| 81 | |
| 82 | pub fn as_str(&self) -> &str { |
| 83 | &self.0 |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 91 | #[sqlx(transparent)] |
| 92 | pub struct FileContentType(String); |
| 93 | |
| 94 | impl FileContentType { |
| 95 | pub fn try_new(value: &str) -> Result<Self, ValidationError> { |
| 96 | let value = value.trim(); |
| 97 | if value.is_empty() { |
| 98 | return Ok(Self("application/octet-stream".to_string())); |
| 99 | } |
| 100 | if value.chars().count() > MAX_FILE_CONTENT_TYPE_LEN { |
| 101 | return Err(ValidationError::TooLong { |
| 102 | field: "content_type", |
| 103 | max: MAX_FILE_CONTENT_TYPE_LEN, |
| 104 | got: value.chars().count(), |
| 105 | }); |
| 106 | } |
| 107 | if !value.is_ascii() { |
| 108 | return Err(ValidationError::NotAscii("content_type")); |
| 109 | } |
| 110 | if value.chars().any(|c| c.is_ascii_control()) { |
| 111 | return Err(ValidationError::Invalid { |
| 112 | field: "content_type", |
| 113 | reason: "must not contain control characters", |
| 114 | }); |
| 115 | } |
| 116 | Ok(Self(value.to_string())) |
| 117 | } |
| 118 | |
| 119 | pub fn as_str(&self) -> &str { |
| 120 | &self.0 |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #[derive(Debug, Clone, sqlx::FromRow)] |
| 125 | pub struct NoteFile { |
| 126 | pub(crate) id: NoteFileId, |
| 127 | pub(crate) note: NoteId, |
| 128 | pub(crate) name: FileName, |
| 129 | pub(crate) content_type: FileContentType, |
| 130 | pub(crate) size: i64, |
| 131 | } |
| 132 | |
| 133 | impl NoteFile { |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | pub fn new(note: &NoteId, name: FileName, content_type: FileContentType, size: i64) -> Self { |
| 139 | Self { |
| 140 | id: NoteFileId::generate(), |
| 141 | note: *note, |
| 142 | name, |
| 143 | content_type, |
| 144 | size, |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | pub fn get_id(&self) -> &NoteFileId { |
| 149 | &self.id |
| 150 | } |
| 151 | |
| 152 | pub fn get_name(&self) -> &FileName { |
| 153 | &self.name |
| 154 | } |
| 155 | |
| 156 | pub fn get_content_type(&self) -> &FileContentType { |
| 157 | &self.content_type |
| 158 | } |
| 159 | |
| 160 | pub fn get_size(&self) -> i64 { |
| 161 | self.size |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | #[cfg(test)] |
| 166 | mod tests { |
| 167 | use super::*; |
| 168 | |
| 169 | #[tokio::test] |
| 170 | async fn filename_rules() { |
| 171 | assert_eq!( |
| 172 | FileName::try_new(" ödev.pdf ").unwrap().as_str(), |
| 173 | "ödev.pdf" |
| 174 | ); |
| 175 | assert!(FileName::try_new("").is_err()); |
| 176 | assert!(FileName::try_new(" ").is_err()); |
| 177 | assert!(FileName::try_new(&"x".repeat(256)).is_err()); |
| 178 | |
| 179 | assert!(FileName::try_new("a/b.pdf").is_err()); |
| 180 | assert!(FileName::try_new("a\\b.pdf").is_err()); |
| 181 | assert!(FileName::try_new("a\nb.pdf").is_err()); |
| 182 | assert!(FileName::try_new("a\x00b").is_err()); |
| 183 | } |
| 184 | |
| 185 | #[tokio::test] |
| 186 | async fn content_type_rules() { |
| 187 | assert_eq!( |
| 188 | FileContentType::try_new("application/pdf") |
| 189 | .unwrap() |
| 190 | .as_str(), |
| 191 | "application/pdf" |
| 192 | ); |
| 193 | |
| 194 | assert_eq!( |
| 195 | FileContentType::try_new("").unwrap().as_str(), |
| 196 | "application/octet-stream" |
| 197 | ); |
| 198 | assert!(FileContentType::try_new(&"x".repeat(101)).is_err()); |
| 199 | assert!(FileContentType::try_new("appli¢ation/pdf").is_err()); |
| 200 | assert!(FileContentType::try_new("a\r\nb").is_err()); |
| 201 | } |
| 202 | } |