i18n.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright (C) 2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package util
  15. import (
  16. "encoding/json"
  17. "errors"
  18. )
  19. // localization id for the Web frontend
  20. const (
  21. I18nSetupTitle = "title.setup"
  22. I18nLoginTitle = "title.login"
  23. I18nShareLoginTitle = "title.share_login"
  24. I18nFilesTitle = "title.files"
  25. I18nSharesTitle = "title.shares"
  26. I18nShareAddTitle = "title.add_share"
  27. I18nShareUpdateTitle = "title.update_share"
  28. I18nProfileTitle = "title.profile"
  29. I18nUsersTitle = "title.users"
  30. I18nGroupsTitle = "title.groups"
  31. I18nFoldersTitle = "title.folders"
  32. I18nChangePwdTitle = "title.change_password"
  33. I18n2FATitle = "title.two_factor_auth"
  34. I18nEditFileTitle = "title.edit_file"
  35. I18nViewFileTitle = "title.view_file"
  36. I18nForgotPwdTitle = "title.recovery_password"
  37. I18nResetPwdTitle = "title.reset_password"
  38. I18nSharedFilesTitle = "title.shared_files"
  39. I18nShareUploadTitle = "title.upload_to_share"
  40. I18nShareDownloadTitle = "title.download_shared_file"
  41. I18nShareAccessErrorTitle = "title.share_access_error"
  42. I18nInvalidAuthReqTitle = "title.invalid_auth_request"
  43. I18nError403Title = "title.error403"
  44. I18nError400Title = "title.error400"
  45. I18nError404Title = "title.error404"
  46. I18nError416Title = "title.error416"
  47. I18nError429Title = "title.error429"
  48. I18nError500Title = "title.error500"
  49. I18nErrorPDFTitle = "title.errorPDF"
  50. I18nErrorEditorTitle = "title.error_editor"
  51. I18nAddUserTitle = "title.add_user"
  52. I18nUpdateUserTitle = "title.update_user"
  53. I18nTemplateUserTitle = "title.template_user"
  54. I18nErrorSetupInstallCode = "setup.install_code_mismatch"
  55. I18nInvalidAuth = "general.invalid_auth_request"
  56. I18nError429Message = "general.error429"
  57. I18nError400Message = "general.error400"
  58. I18nError403Message = "general.error403"
  59. I18nError404Message = "general.error404"
  60. I18nError416Message = "general.error416"
  61. I18nError500Message = "general.error500"
  62. I18nErrorPDFMessage = "general.errorPDF"
  63. I18nErrorInvalidToken = "general.invalid_token"
  64. I18nErrorInvalidForm = "general.invalid_form"
  65. I18nErrorInvalidCredentials = "general.invalid_credentials"
  66. I18nErrorInvalidCSRF = "general.invalid_csrf"
  67. I18nErrorFsGeneric = "fs.err_generic"
  68. I18nErrorDirListGeneric = "fs.dir_list.err_generic"
  69. I18nErrorDirList403 = "fs.dir_list.err_403"
  70. I18nErrorDirList429 = "fs.dir_list.err_429"
  71. I18nErrorDirListUser = "fs.dir_list.err_user"
  72. I18nErrorFsValidation = "fs.err_validation"
  73. I18nErrorChangePwdRequiredFields = "change_pwd.required_fields"
  74. I18nErrorChangePwdNoMatch = "change_pwd.no_match"
  75. I18nErrorChangePwdGeneric = "change_pwd.generic"
  76. I18nErrorChangePwdNoDifferent = "change_pwd.no_different"
  77. I18nErrorChangePwdCurrentNoMatch = "change_pwd.current_no_match"
  78. I18nErrorChangePwdRequired = "change_pwd.required"
  79. I18nErrorUsernameRequired = "general.username_required"
  80. I18nErrorGetUser = "general.err_user"
  81. I18nErrorPwdResetForbidded = "login.reset_pwd_forbidden"
  82. I18nErrorPwdResetNoEmail = "login.reset_pwd_no_email"
  83. I18nErrorPwdResetSendEmail = "login.reset_pwd_send_email_err"
  84. I18nErrorPwdResetGeneric = "login.reset_pwd_err_generic"
  85. I18nErrorProtocolForbidden = "general.err_protocol_forbidden"
  86. I18nErrorPwdLoginForbidden = "general.pwd_login_forbidden"
  87. I18nErrorIPForbidden = "general.ip_forbidden"
  88. I18nErrorConnectionForbidden = "general.connection_forbidden"
  89. I18nErrorReservedUsername = "user.username_reserved"
  90. I18nErrorInvalidEmail = "general.email_invalid"
  91. I18nErrorInvalidUser = "user.username_invalid"
  92. I18nErrorInvalidName = "user.name_invalid"
  93. I18nErrorHomeRequired = "user.home_required"
  94. I18nErrorHomeInvalid = "user.home_invalid"
  95. I18nErrorPubKeyInvalid = "user.pub_key_invalid"
  96. I18nErrorPrimaryGroup = "user.err_primary_group"
  97. I18nErrorDuplicateGroup = "user.err_duplicate_group"
  98. I18nErrorNoPermission = "user.no_permissions"
  99. I18nErrorNoRootPermission = "user.no_root_permissions"
  100. I18nErrorGenericPermission = "user.err_permissions_generic"
  101. I18nError2FAInvalid = "user.2fa_invalid"
  102. I18nErrorRecoveryCodesInvalid = "user.recovery_codes_invalid"
  103. I18nErrorFolderNameRequired = "general.foldername_required"
  104. I18nErrorFolderMountPathRequired = "user.folder_path_required"
  105. I18nErrorDuplicatedFolders = "user.folder_duplicated"
  106. I18nErrorOverlappedFolders = "user.folder_overlapped"
  107. I18nErrorFolderQuotaSizeInvalid = "user.folder_quota_size_invalid"
  108. I18nErrorFolderQuotaFileInvalid = "user.folder_quota_file_invalid"
  109. I18nErrorFolderQuotaInvalid = "user.folder_quota_invalid"
  110. I18nErrorPasswordComplexity = "general.err_password_complexity"
  111. I18nErrorIPFiltersInvalid = "user.ip_filters_invalid"
  112. I18nErrorSourceBWLimitInvalid = "user.src_bw_limits_invalid"
  113. I18nErrorShareExpirationInvalid = "user.share_expiration_invalid"
  114. I18nErrorFilePatternPathInvalid = "user.file_pattern_path_invalid"
  115. I18nErrorFilePatternDuplicated = "user.file_pattern_duplicated"
  116. I18nErrorFilePatternInvalid = "user.file_pattern_invalid"
  117. I18nErrorDisableActive2FA = "user.disable_active_2fa"
  118. I18nErrorPwdChangeConflict = "user.pwd_change_conflict"
  119. I18nErrorLoginAfterReset = "login.reset_ok_login_error"
  120. I18nErrorShareScope = "share.scope_invalid"
  121. I18nErrorShareMaxTokens = "share.max_tokens_invalid"
  122. I18nErrorShareExpiration = "share.expiration_invalid"
  123. I18nErrorShareNoPwd = "share.err_no_password"
  124. I18nErrorShareExpirationOutOfRange = "share.expiration_out_of_range"
  125. I18nErrorShareGeneric = "share.generic"
  126. I18nErrorNameRequired = "general.name_required"
  127. I18nErrorSharePathRequired = "share.path_required"
  128. I18nErrorShareWriteScope = "share.path_write_scope"
  129. I18nErrorShareNestedPaths = "share.nested_paths"
  130. I18nErrorShareExpirationPast = "share.expiration_past"
  131. I18nErrorInvalidIPMask = "general.allowed_ip_mask_invalid"
  132. I18nErrorShareUsage = "share.usage_exceed"
  133. I18nErrorShareExpired = "share.expired"
  134. I18nErrorLoginFromIPDenied = "login.ip_not_allowed"
  135. I18nError2FARequired = "login.two_factor_required"
  136. I18nErrorNoOIDCFeature = "general.no_oidc_feature"
  137. I18nErrorNoPermissions = "general.no_permissions"
  138. I18nErrorShareBrowsePaths = "share.browsable_multiple_paths"
  139. I18nErrorShareBrowseNoDir = "share.browsable_non_dir"
  140. I18nErrorShareInvalidPath = "share.invalid_path"
  141. I18nErrorPathInvalid = "general.path_invalid"
  142. I18nErrorQuotaRead = "general.err_quota_read"
  143. I18nErrorEditDir = "general.error_edit_dir"
  144. I18nErrorEditSize = "general.error_edit_size"
  145. I18nProfileUpdated = "general.profile_updated"
  146. I18nShareLoginOK = "general.share_ok"
  147. I18n2FADisabled = "2fa.disabled"
  148. I18nOIDCTokenExpired = "oidc.token_expired"
  149. I18nOIDCTokenInvalidAdmin = "oidc.token_invalid_webadmin"
  150. I18nOIDCTokenInvalidUser = "oidc.token_invalid_webclient"
  151. I18nOIDCErrTokenExchange = "oidc.token_exchange_err"
  152. I18nOIDCTokenInvalid = "oidc.token_invalid"
  153. I18nOIDCTokenInvalidRoleAdmin = "oidc.role_admin_err"
  154. I18nOIDCTokenInvalidRoleUser = "oidc.role_user_err"
  155. I18nOIDCErrGetUser = "oidc.get_user_err"
  156. I18nStorageLocal = "storage.local"
  157. I18nStorageLocalEncrypted = "storage.encrypted"
  158. I18nStorageS3 = "storage.s3"
  159. I18nStorageGCS = "storage.gcs"
  160. I18nStorageAzureBlob = "storage.azblob"
  161. I18nStorageSFTP = "storage.sftp"
  162. I18nStorageHTTP = "storage.http"
  163. I18nErrorInvalidQuotaSize = "user.invalid_quota_size"
  164. I18nErrorInvalidMaxFilesize = "filters.max_upload_size_invalid"
  165. I18nErrorInvalidHomeDir = "storage.home_dir_invalid"
  166. I18nErrorBucketRequired = "storage.bucket_required"
  167. I18nErrorRegionRequired = "storage.region_required"
  168. I18nErrorKeyPrefixInvalid = "storage.key_prefix_invalid"
  169. I18nErrorULPartSizeInvalid = "storage.ul_part_size_invalid"
  170. I18nErrorDLPartSizeInvalid = "storage.dl_part_size_invalid"
  171. I18nErrorULConcurrencyInvalid = "storage.ul_concurrency_invalid"
  172. I18nErrorDLConcurrencyInvalid = "storage.dl_concurrency_invalid"
  173. I18nErrorAccessKeyRequired = "storage.access_key_required"
  174. I18nErrorAccessSecretRequired = "storage.access_secret_required"
  175. I18nErrorFsCredentialsRequired = "storage.credentials_required"
  176. I18nErrorContainerRequired = "storage.container_required"
  177. I18nErrorAccountNameRequired = "storage.account_name_required"
  178. I18nErrorSASURLInvalid = "storage.sas_url_invalid"
  179. I18nErrorPassphraseRequired = "storage.passphrase_required"
  180. I18nErrorEndpointInvalid = "storage.endpoint_invalid"
  181. I18nErrorEndpointRequired = "storage.endpoint_required"
  182. I18nErrorFsUsernameRequired = "storage.username_required"
  183. I18nAddGroupTitle = "title.add_group"
  184. I18nUpdateGroupTitle = "title.update_group"
  185. I18nErrorInvalidTLSCert = "user.tls_cert_invalid"
  186. I18nAddFolderTitle = "title.add_folder"
  187. I18nUpdateFolderTitle = "title.update_folder"
  188. I18nTemplateFolderTitle = "title.template_folder"
  189. )
  190. // NewI18nError returns a I18nError wrappring the provided error
  191. func NewI18nError(err error, message string, options ...I18nErrorOption) *I18nError {
  192. var errI18n *I18nError
  193. if errors.As(err, &errI18n) {
  194. return errI18n
  195. }
  196. errI18n = &I18nError{
  197. err: err,
  198. Message: message,
  199. args: nil,
  200. }
  201. for _, opt := range options {
  202. opt(errI18n)
  203. }
  204. return errI18n
  205. }
  206. // I18nErrorOption defines a functional option type that allows to configure the I18nError.
  207. type I18nErrorOption func(*I18nError)
  208. // I18nErrorArgs is a functional option to set I18nError arguments.
  209. func I18nErrorArgs(args map[string]any) I18nErrorOption {
  210. return func(e *I18nError) {
  211. e.args = args
  212. }
  213. }
  214. // I18nError is an error wrapper that add a message to use for localization.
  215. type I18nError struct {
  216. err error
  217. Message string
  218. args map[string]any
  219. }
  220. // Error returns the wrapped error string.
  221. func (e *I18nError) Error() string {
  222. return e.err.Error()
  223. }
  224. // Unwrap returns the underlying error
  225. func (e *I18nError) Unwrap() error {
  226. return e.err
  227. }
  228. // Is reports if target matches
  229. func (e *I18nError) Is(target error) bool {
  230. if errors.Is(e.err, target) {
  231. return true
  232. }
  233. _, ok := target.(*I18nError)
  234. return ok
  235. }
  236. // HasArgs returns true if the error has i18n args.
  237. func (e *I18nError) HasArgs() bool {
  238. return len(e.args) > 0
  239. }
  240. // Args returns the provided args in JSON format
  241. func (e *I18nError) Args() string {
  242. if len(e.args) > 0 {
  243. data, err := json.Marshal(e.args)
  244. if err == nil {
  245. return string(data)
  246. }
  247. }
  248. return "{}"
  249. }