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 {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' | ||||
}; | }; | ||||
export async function vaultRequest(objectType, objectId) { | export async function vaultRequest(objectType, swhid) { | ||||
let vaultUrl; | let vaultUrl; | ||||
if (objectType === 'directory') { | if (objectType === 'directory') { | ||||
vaultUrl = Urls.api_1_vault_cook_directory(objectId); | vaultUrl = Urls.api_1_vault_cook_flat(swhid); | ||||
} else { | } else { | ||||
vaultUrl = Urls.api_1_vault_cook_revision_gitfast(objectId); | vaultUrl = Urls.api_1_vault_cook_gitfast(swhid); | ||||
} | } | ||||
// check if object has already been cooked | // check if object has already been cooked | ||||
const response = await fetch(vaultUrl); | const response = await fetch(vaultUrl); | ||||
const data = await response.json(); | const data = await response.json(); | ||||
// object needs to be cooked | // object needs to be cooked | ||||
if (data.exception === 'NotFoundExc' || data.status === 'failed') { | if (data.exception === 'NotFoundExc' || data.status === 'failed') { | ||||
// if last cooking has failed, remove previous task info from localStorage | // if last cooking has failed, remove previous task info from localStorage | ||||
// in order to force the recooking of the object | // in order to force the recooking of the object | ||||
swh.vault.removeCookingTaskInfo([objectId]); | swh.vault.removeCookingTaskInfo([swhid]); | ||||
$(`#vault-cook-${objectType}-modal`).modal('show'); | $(`#vault-cook-${objectType}-modal`).modal('show'); | ||||
// object has been cooked and should be in the vault cache, | // object has been cooked and should be in the vault cache, | ||||
// it will be asked to cook it again if it is not | // it will be asked to cook it again if it is not | ||||
} else if (data.status === 'done') { | } else if (data.status === 'done') { | ||||
$(`#vault-fetch-${objectType}-modal`).modal('show'); | $(`#vault-fetch-${objectType}-modal`).modal('show'); | ||||
} else { | } else { | ||||
const cookingServiceDownAlert = | const cookingServiceDownAlert = | ||||
$(htmlAlert('danger', | $(htmlAlert('danger', | ||||
'Archive cooking service is currently experiencing issues.<br/>' + | 'Archive cooking service is currently experiencing issues.<br/>' + | ||||
'Please try again later.', | 'Please try again later.', | ||||
true)); | true)); | ||||
cookingServiceDownAlert.css(alertStyle); | cookingServiceDownAlert.css(alertStyle); | ||||
$('body').append(cookingServiceDownAlert); | $('body').append(cookingServiceDownAlert); | ||||
} | } | ||||
} | } | ||||
async function addVaultCookingTask(cookingTask) { | async function addVaultCookingTask(objectType, cookingTask) { | ||||
const swhidsContext = swh.webapp.getSwhIdsContext(); | const swhidsContext = swh.webapp.getSwhIdsContext(); | ||||
cookingTask.origin = swhidsContext[cookingTask.object_type].context.origin; | cookingTask.origin = swhidsContext[objectType].context.origin; | ||||
cookingTask.path = swhidsContext[cookingTask.object_type].context.path; | cookingTask.path = swhidsContext[objectType].context.path; | ||||
cookingTask.browse_url = swhidsContext[cookingTask.object_type].swhid_with_context_url; | cookingTask.browse_url = swhidsContext[objectType].swhid_with_context_url; | ||||
if (!cookingTask.browse_url) { | if (!cookingTask.browse_url) { | ||||
cookingTask.browse_url = swhidsContext[cookingTask.object_type].swhid_url; | cookingTask.browse_url = swhidsContext[objectType].swhid_url; | ||||
} | } | ||||
let vaultCookingTasks = JSON.parse(localStorage.getItem('swh-vault-cooking-tasks')); | let vaultCookingTasks = JSON.parse(localStorage.getItem('swh-vault-cooking-tasks')); | ||||
if (!vaultCookingTasks) { | if (!vaultCookingTasks) { | ||||
vaultCookingTasks = []; | vaultCookingTasks = []; | ||||
} | } | ||||
if (vaultCookingTasks.find(val => { | if (vaultCookingTasks.find(val => { | ||||
return val.object_type === cookingTask.object_type && | return val.bundle_type === cookingTask.bundle_type && | ||||
val.object_id === cookingTask.object_id; | val.swhid === cookingTask.swhid; | ||||
}) === undefined) { | }) === undefined) { | ||||
let cookingUrl; | let cookingUrl; | ||||
if (cookingTask.object_type === 'directory') { | if (cookingTask.bundle_type === 'flat') { | ||||
cookingUrl = Urls.api_1_vault_cook_directory(cookingTask.object_id); | cookingUrl = Urls.api_1_vault_cook_flat(cookingTask.swhid); | ||||
} else { | } else { | ||||
cookingUrl = Urls.api_1_vault_cook_revision_gitfast(cookingTask.object_id); | cookingUrl = Urls.api_1_vault_cook_gitfast(cookingTask.swhid); | ||||
} | } | ||||
anlambert: As the old JSON schema for cooking tasks is still used in production and stored in local… | |||||
if (cookingTask.email) { | if (cookingTask.email) { | ||||
cookingUrl += '?email=' + cookingTask.email; | cookingUrl += '?email=' + cookingTask.email; | ||||
} | } | ||||
try { | try { | ||||
const response = await csrfPost(cookingUrl); | const response = await csrfPost(cookingUrl); | ||||
handleFetchError(response); | handleFetchError(response); | ||||
vaultCookingTasks.push(cookingTask); | vaultCookingTasks.push(cookingTask); | ||||
Show All 21 Lines | async function addVaultCookingTask(objectType, cookingTask) { | ||||
} | } | ||||
} | } | ||||
function validateEmail(email) { | 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,}))$/; | 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()); | return re.test(String(email).toLowerCase()); | ||||
} | } | ||||
export function cookDirectoryArchive(directoryId) { | 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 || validateEmail(email)) { | ||||
const cookingTask = { | const cookingTask = { | ||||
'object_type': 'directory', | 'bundle_type': 'flat', | ||||
'object_id': directoryId, | 'swhid': swhid, | ||||
'email': email, | 'email': email, | ||||
'status': 'new' | 'status': 'new' | ||||
}; | }; | ||||
addVaultCookingTask(cookingTask); | addVaultCookingTask('directory', cookingTask); | ||||
} else { | } else { | ||||
$('#invalid-email-modal').modal('show'); | $('#invalid-email-modal').modal('show'); | ||||
} | } | ||||
} | } | ||||
export async function fetchDirectoryArchive(directoryId) { | export async function fetchDirectoryArchive(directorySwhid) { | ||||
$('#vault-fetch-directory-modal').modal('hide'); | $('#vault-fetch-directory-modal').modal('hide'); | ||||
const vaultUrl = Urls.api_1_vault_cook_directory(directoryId); | 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 || validateEmail(email)) { | ||||
const cookingTask = { | const cookingTask = { | ||||
'object_type': 'revision', | 'bundle_type': 'gitfast', | ||||
'object_id': revisionId, | 'swhid': revisionId, | ||||
'email': email, | 'email': email, | ||||
'status': 'new' | 'status': 'new' | ||||
}; | }; | ||||
addVaultCookingTask(cookingTask); | addVaultCookingTask('revision', cookingTask); | ||||
} else { | } else { | ||||
$('#invalid-email-modal').modal('show'); | $('#invalid-email-modal').modal('show'); | ||||
} | } | ||||
} | } | ||||
export async function fetchRevisionArchive(revisionId) { | export async function fetchRevisionArchive(revisionSwhid) { | ||||
$('#vault-fetch-directory-modal').modal('hide'); | $('#vault-fetch-directory-modal').modal('hide'); | ||||
const vaultUrl = Urls.api_1_vault_cook_revision_gitfast(revisionId); | const vaultUrl = Urls.api_1_vault_cook_gitfast(revisionSwhid); | ||||
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); | ||||
} | } |
As the old JSON schema for cooking tasks is still used in production and stored in local browser storage, you should add backward compatibility for it otherwise errors will appear for users that already used the vault UI.