Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 9bf28404 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

REPO: Make a kara unavailable when we download it

parent 542198aa
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -212,8 +212,30 @@ impl LktDatabaseConnection { ...@@ -212,8 +212,30 @@ impl LktDatabaseConnection {
}) })
} }
pub fn make_kara_available<'a>(&mut self, remote_id: RemoteKaraId) -> LktDatabaseResult<()> { /// Makes a kara avilable, set the avail flag to `true` or `false` depending on the passed
todo!() /// flag. If the operation changed nothing, don't raise an error.
pub fn make_kara_available<'a>(
&mut self,
remote_id: RemoteKaraId,
flag: bool,
) -> LktDatabaseResult<()> {
let RemoteKaraId {
repo_id: rid,
repo_kara_id: rkid,
} = remote_id;
self.sqlite.exclusive_transaction(|c| {
let local_kara_id: i64 = with_dsl!(repo_kara => repo_kara
.select(local_kara_id)
.filter(repo_id.eq(rid).and(repo_kara_id.eq(rkid)))
.first(c)?
);
with_dsl!(kara => diesel::update(kara)
.filter(id.eq(local_kara_id))
.set(is_dl.eq(flag))
.execute(c)?
);
Ok(())
})
} }
/// Create a series of models from a kara signature from Kurisu's V1 API. /// Create a series of models from a kara signature from Kurisu's V1 API.
......
...@@ -18,7 +18,7 @@ pub struct KaraId { ...@@ -18,7 +18,7 @@ pub struct KaraId {
pub local_kara_id: i64, pub local_kara_id: i64,
} }
#[derive(Debug, Queryable, Selectable)] #[derive(Debug, Queryable, Selectable, Clone, Copy)]
#[diesel(table_name = repo_kara)] #[diesel(table_name = repo_kara)]
pub struct RemoteKaraId { pub struct RemoteKaraId {
pub repo_id: i64, pub repo_id: i64,
......
...@@ -225,24 +225,42 @@ impl Download { ...@@ -225,24 +225,42 @@ impl Download {
search_urls: &[&str], search_urls: &[&str],
dest: impl AsRef<Path>, dest: impl AsRef<Path>,
) { ) {
let (dest, name, db) = (dest.as_ref(), name.as_ref(), self.db.clone()); macro_rules! make_kara_available {
if let Err(err) = ($db: expr, $id: expr, $flag: expr) => {{
Download::download_kara(db.clone(), api, search_urls, repo_id, repo_kara_id, dest).await let mut db = $db.lock().expect("failed to lock the database...");
{ if let Err(err) = db.make_kara_available($id, $flag) {
log::error!(target: "REPO", "failed to download file `{}` for kara {repo_kara_id}: {err}", dest.to_string_lossy()); log::error!(
target: "REPO",
"failed to make kara `{repo_kara_id}` from {} {}: {err}",
name.as_ref(),
either!($flag => "available"; "unavailable")
);
return; return;
} }
let mut db = db.lock().expect("failed to lock the database..."); }};
let remote_id = RemoteKaraId { }
let (remote_id, db, dest) = (
RemoteKaraId {
repo_id, repo_id,
repo_kara_id, repo_kara_id,
}; },
if let Err(err) = db.make_kara_available(remote_id) { self.db.clone(),
log::error!(target: "REPO", "failed to make kara `{repo_kara_id}` from {name} available: {err}"); dest.as_ref(),
);
make_kara_available!(db, remote_id, false);
if let Err(err) =
Download::download_kara(db.clone(), api, search_urls, repo_id, repo_kara_id, dest).await
{
log::error!(target: "REPO", "failed to download file `{}` for kara {repo_kara_id}: {err}", dest.to_string_lossy());
return; return;
} }
make_kara_available!(db, remote_id, true);
log::info!( log::info!(
"downloaded kara {repo_kara_id} from {name} at location {}", "downloaded kara {repo_kara_id} from {} at location {}",
name.as_ref(),
dest.to_string_lossy() dest.to_string_lossy()
); );
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter