]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/helpers/utils.ts
Add ability to set a custom quota
[github/Chocobozzz/PeerTube.git] / client / src / app / helpers / utils.ts
index aa37fdd46b8dfd70bbcbcca66621bca66d561c1a..6d7e76b1168d7de3b20900ac61eecc0cb5826a04 100644 (file)
@@ -1,4 +1,8 @@
+import { SelectChannelItem } from 'src/types/select-options-item.model'
 import { DatePipe } from '@angular/common'
+import { HttpErrorResponse } from '@angular/common/http'
+import { Notifier } from '@app/core'
+import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
 import { environment } from '../../environments/environment'
 import { AuthService } from '../core/auth'
 
@@ -18,9 +22,9 @@ function getParameterByName (name: string, url: string) {
 
 function populateAsyncUserVideoChannels (
   authService: AuthService,
-  channel: { id: number, label: string, support?: string, avatarPath?: string, recent?: boolean }[]
+  channel: SelectChannelItem[]
 ) {
-  return new Promise(res => {
+  return new Promise<void>(res => {
     authService.userInformationLoaded
       .subscribe(
         () => {
@@ -57,7 +61,7 @@ function getAbsoluteAPIUrl () {
 }
 
 function getAbsoluteEmbedUrl () {
-  let absoluteEmbedUrl = environment.embedUrl
+  let absoluteEmbedUrl = environment.originServerUrl
   if (!absoluteEmbedUrl) {
     // The Embed is on the same domain
     absoluteEmbedUrl = window.location.origin
@@ -143,42 +147,11 @@ function sortBy (obj: any[], key1: string, key2?: string) {
   })
 }
 
-function scrollToTop () {
-  window.scroll(0, 0)
-}
-
-// Thanks: https://github.com/uupaa/dynamic-import-polyfill
-function importModule (path: string) {
-  return new Promise((resolve, reject) => {
-    const vector = '$importModule$' + Math.random().toString(32).slice(2)
-    const script = document.createElement('script')
-
-    const destructor = () => {
-      delete window[ vector ]
-      script.onerror = null
-      script.onload = null
-      script.remove()
-      URL.revokeObjectURL(script.src)
-      script.src = ''
-    }
-
-    script.defer = true
-    script.type = 'module'
-
-    script.onerror = () => {
-      reject(new Error(`Failed to import: ${path}`))
-      destructor()
-    }
-    script.onload = () => {
-      resolve(window[ vector ])
-      destructor()
-    }
-    const absURL = (environment.apiUrl || window.location.origin) + path
-    const loader = `import * as m from "${absURL}"; window.${vector} = m;` // export Module
-    const blob = new Blob([ loader ], { type: 'text/javascript' })
-    script.src = URL.createObjectURL(blob)
-
-    document.head.appendChild(script)
+function scrollToTop (behavior: 'auto' | 'smooth' = 'auto') {
+  window.scrollTo({
+    left: 0,
+    top: 0,
+    behavior
   })
 }
 
@@ -202,6 +175,33 @@ function isXPercentInViewport (el: HTMLElement, percentVisible: number) {
   )
 }
 
+function uploadErrorHandler (parameters: {
+  err: HttpErrorResponse
+  name: string
+  notifier: Notifier
+  sticky?: boolean
+}) {
+  const { err, name, notifier, sticky } = { sticky: false, ...parameters }
+  const title = $localize`The upload failed`
+  let message = err.message
+
+  if (err instanceof ErrorEvent) { // network error
+    message = $localize`The connection was interrupted`
+    notifier.error(message, title, null, sticky)
+  } else if (err.status === HttpStatusCode.REQUEST_TIMEOUT_408) {
+    message = $localize`Your ${name} file couldn't be transferred before the set timeout (usually 10min)`
+    notifier.error(message, title, null, sticky)
+  } else if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413) {
+    const maxFileSize = err.headers?.get('X-File-Maximum-Size') || '8G'
+    message = $localize`Your ${name} file was too large (max. size: ${maxFileSize})`
+    notifier.error(message, title, null, sticky)
+  } else {
+    notifier.error(err.message, title)
+  }
+
+  return message
+}
+
 export {
   sortBy,
   durationToString,
@@ -215,8 +215,8 @@ export {
   getAbsoluteEmbedUrl,
   objectLineFeedToHtml,
   removeElementFromArray,
-  importModule,
   scrollToTop,
   isInViewport,
-  isXPercentInViewport
+  isXPercentInViewport,
+  uploadErrorHandler
 }