Changeset View
Changeset View
Standalone View
Standalone View
assets/src/bundles/vault/vault-create-tasks.js
/** | /** | ||||
* Copyright (C) 2018-2019 The Software Heritage developers | * Copyright (C) 2018-2019 The Software Heritage developers | ||||
* See the AUTHORS file at the top-level directory of this distribution | * See the AUTHORS file at the top-level directory of this distribution | ||||
* License: GNU Affero General Public License version 3, or any later version | * License: GNU Affero General Public License version 3, or any later version | ||||
* See top-level LICENSE file for more information | * See top-level LICENSE file for more information | ||||
*/ | */ | ||||
import * as EmailValidator from 'email-validator'; | |||||
import {handleFetchError, csrfPost, htmlAlert} from 'utils/functions'; | import {handleFetchError, csrfPost, htmlAlert} from 'utils/functions'; | ||||
const alertStyle = { | const alertStyle = { | ||||
'position': 'fixed', | 'position': 'fixed', | ||||
'left': '1rem', | 'left': '1rem', | ||||
'bottom': '1rem', | 'bottom': '1rem', | ||||
'z-index': '100000' | 'z-index': '100000' | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | async function addVaultCookingTask(objectType, cookingTask) { | ||||
}) === undefined) { | }) === undefined) { | ||||
let cookingUrl; | let cookingUrl; | ||||
if (cookingTask.bundle_type === 'flat') { | if (cookingTask.bundle_type === 'flat') { | ||||
cookingUrl = Urls.api_1_vault_cook_flat(cookingTask.swhid); | cookingUrl = Urls.api_1_vault_cook_flat(cookingTask.swhid); | ||||
} else { | } else { | ||||
cookingUrl = Urls.api_1_vault_cook_git_bare(cookingTask.swhid); | cookingUrl = Urls.api_1_vault_cook_git_bare(cookingTask.swhid); | ||||
} | } | ||||
if (cookingTask.email) { | if (cookingTask.email) { | ||||
cookingUrl += '?email=' + cookingTask.email; | cookingUrl += '?email=' + encodeURIComponent(cookingTask.email); | ||||
} | } | ||||
try { | try { | ||||
const response = await csrfPost(cookingUrl); | const response = await csrfPost(cookingUrl); | ||||
handleFetchError(response); | handleFetchError(response); | ||||
vaultCookingTasks.push(cookingTask); | vaultCookingTasks.push(cookingTask); | ||||
localStorage.setItem('swh-vault-cooking-tasks', JSON.stringify(vaultCookingTasks)); | localStorage.setItem('swh-vault-cooking-tasks', JSON.stringify(vaultCookingTasks)); | ||||
$('#vault-cook-directory-modal').modal('hide'); | $('#vault-cook-directory-modal').modal('hide'); | ||||
Show All 14 Lines | try { | ||||
'Archive cooking request submission failed.', | 'Archive cooking request submission failed.', | ||||
true)); | true)); | ||||
cookingTaskFailedAlert.css(alertStyle); | cookingTaskFailedAlert.css(alertStyle); | ||||
$('body').append(cookingTaskFailedAlert); | $('body').append(cookingTaskFailedAlert); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
function validateEmail(email) { | |||||
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |||||
return re.test(String(email).toLowerCase()); | |||||
} | |||||
export function cookDirectoryArchive(swhid) { | export function cookDirectoryArchive(swhid) { | ||||
const email = $('#swh-vault-directory-email').val().trim(); | const email = $('#swh-vault-directory-email').val().trim(); | ||||
if (!email || validateEmail(email)) { | if (!email || EmailValidator.validate(email)) { | ||||
const cookingTask = { | const cookingTask = { | ||||
'bundle_type': 'flat', | 'bundle_type': 'flat', | ||||
'swhid': swhid, | 'swhid': swhid, | ||||
'email': email, | 'email': email, | ||||
'status': 'new' | 'status': 'new' | ||||
}; | }; | ||||
addVaultCookingTask('directory', cookingTask); | addVaultCookingTask('directory', cookingTask); | ||||
} else { | } else { | ||||
$('#invalid-email-modal').modal('show'); | $('#invalid-email-modal').modal('show'); | ||||
} | } | ||||
} | } | ||||
export async function fetchDirectoryArchive(directorySwhid) { | export async function fetchDirectoryArchive(directorySwhid) { | ||||
$('#vault-fetch-directory-modal').modal('hide'); | $('#vault-fetch-directory-modal').modal('hide'); | ||||
const vaultUrl = Urls.api_1_vault_cook_flat(directorySwhid); | const vaultUrl = Urls.api_1_vault_cook_flat(directorySwhid); | ||||
const response = await fetch(vaultUrl); | const response = await fetch(vaultUrl); | ||||
const data = await response.json(); | const data = await response.json(); | ||||
swh.vault.fetchCookedObject(data.fetch_url); | swh.vault.fetchCookedObject(data.fetch_url); | ||||
} | } | ||||
export function cookRevisionArchive(revisionId) { | export function cookRevisionArchive(revisionId) { | ||||
const email = $('#swh-vault-revision-email').val().trim(); | const email = $('#swh-vault-revision-email').val().trim(); | ||||
if (!email || validateEmail(email)) { | if (!email || EmailValidator.validate(email)) { | ||||
const cookingTask = { | const cookingTask = { | ||||
'bundle_type': 'git_bare', | 'bundle_type': 'git_bare', | ||||
'swhid': revisionId, | 'swhid': revisionId, | ||||
'email': email, | 'email': email, | ||||
'status': 'new' | 'status': 'new' | ||||
}; | }; | ||||
addVaultCookingTask('revision', cookingTask); | addVaultCookingTask('revision', cookingTask); | ||||
} else { | } else { | ||||
Show All 11 Lines |