fix change token not fetched

This commit is contained in:
OMGeeky
2024-04-14 13:25:05 +02:00
parent 3c37a55991
commit 5167c8ebd4
6 changed files with 87 additions and 10 deletions

View File

@@ -13,7 +13,9 @@ use std::fmt::{Debug, Display, Formatter};
use tokio::fs;
const FIELDS_FILE: &'static str = "id, name, size, mimeType, kind, md5Checksum, parents, trashed, createdTime, modifiedTime, viewedByMeTime";
const FIELDS_CHANGE: &str = formatcp!("changes(removed, fileId, changeType, file({FIELDS_FILE}))");
const FIELDS_CHANGE: &str = formatcp!(
"nextPageToken, newStartPageToken, changes(removed, fileId, changeType, file({FIELDS_FILE}))"
);
#[derive(Clone)]
pub struct GoogleDrive {
hub: DriveHub<HttpsConnector<HttpConnector>>,
@@ -80,6 +82,7 @@ impl GoogleDrive {
//region changes
#[instrument]
pub async fn get_changes(&mut self) -> Result<Vec<Change>> {
info!("Getting changes");
let mut page_token = Some(self.change_start_token().await?);
let mut changes = Vec::new();
while let Some(current_page_token) = page_token {
@@ -97,7 +100,9 @@ impl GoogleDrive {
.include_items_from_all_drives(false)
.doit()
.await?;
self.changes_start_page_token = body.new_start_page_token;
if let Some(token) = body.new_start_page_token {
self.changes_start_page_token = Some(token);
}
if response.status().is_success() {
changes.extend(body.changes.unwrap_or_default());
page_token = body.next_page_token;
@@ -113,7 +118,7 @@ impl GoogleDrive {
async fn change_start_token(&mut self) -> Result<String> {
Ok(match &self.changes_start_page_token {
None => {
//
info!("Getting start page token");
let token = fs::read_to_string(SETTINGS.get_changes_file_path())
.await
.unwrap_or_default();
@@ -122,14 +127,19 @@ impl GoogleDrive {
} else {
Some(self.get_changes_start_token_from_api().await?)
};
info!("Got start page token: {:?}", self.changes_start_page_token);
self.changes_start_page_token
.clone()
.expect("We just set it")
}
Some(start_token) => start_token.clone(),
Some(start_token) => {
info!("Using cached start page token");
start_token.clone()
}
})
}
async fn get_changes_start_token_from_api(&self) -> Result<String> {
info!("Getting start page token from API");
let (response, body) = self
.hub
.changes()

View File

@@ -1,5 +1,7 @@
use crate::prelude::*;
use futures::{future, prelude::*};
use gdriver_common::drive_structure::meta;
use gdriver_common::ipc::gdriver_service::SETTINGS;
use std::net::SocketAddr;
use tarpc::{
context,
@@ -19,6 +21,11 @@ pub(crate) async fn spawn(fut: impl Future<Output = ()> + Send + 'static) {
#[tokio::main]
async fn main() -> Result<()> {
gdriver_common::tracing_setup::init_tracing();
SETTINGS.initialize_dirs()?;
let root_meta_file = SETTINGS.get_metadata_file_path(&ROOT_ID);
let root_meta = meta::Metadata::root();
meta::write_metadata_file(&root_meta_file, &root_meta)?;
// sample::main().await?;
service::start().await?;
Ok(())

View File

@@ -58,6 +58,9 @@ impl GDriverService for GdriverServer {
context: Context,
id: DriveId,
) -> StdResult<(), GetMetadataError> {
if id == *ROOT_ID {
return Ok(());
}
Err(GetMetadataError::Other)
}
@@ -119,7 +122,7 @@ impl GDriverService for GdriverServer {
Err(UpdateChangesError::Other)
}
async fn update_changes(self, context: Context) -> StdResult<(), UpdateChangesError> {
async fn update_changes(self, _context: Context) -> StdResult<(), UpdateChangesError> {
let drive = self.drive.try_lock();
match drive {
Ok(mut drive) => {
@@ -128,12 +131,13 @@ impl GDriverService for GdriverServer {
dbg!(e);
UpdateChangesError::Remote
})?;
Ok(())
}
Err(_) => {
return Err(UpdateChangesError::Running);
info!("Drive is already updating");
Err(UpdateChangesError::Running)
}
}
Ok(())
}
async fn do_something2(