diff --git a/R/api_download.R b/R/api_download.R index 051048d62..ae9c87e39 100644 --- a/R/api_download.R +++ b/R/api_download.R @@ -40,6 +40,16 @@ ) # Try to download while (n_tries > 0) { + # Check if the output file already exists + if (.raster_is_valid(output_file)) { + local_asset <- .tile_from_file( + file = output_file, base_tile = asset, + band = .tile_bands(asset), update_bbox = TRUE, + labels = .tile_labels(asset) + ) + + return(local_asset) + } # Update token (for big tiffs and slow networks) asset <- .cube_token_generator(asset) # Crop and download @@ -50,7 +60,7 @@ output_file = output_file, gdal_params = gdal_params ), - default = NULL + .default = NULL ) # Check if the downloaded file is valid if (.has(local_asset) && .raster_is_valid(output_file)) { diff --git a/tests/testthat/test-cube_copy.R b/tests/testthat/test-cube_copy.R index ca593b08f..31316a895 100644 --- a/tests/testthat/test-cube_copy.R +++ b/tests/testthat/test-cube_copy.R @@ -214,3 +214,41 @@ test_that("Copy remote cube works (specific region with resampling)", { unlink(data_dir, recursive = TRUE) }) + +test_that("Copy invalid files", { + data_dir <- system.file("extdata/raster/mod13q1", package = "sits") + + cube <- sits_cube( + source = "BDC", + collection = "MOD13Q1-6.1", + data_dir = data_dir, + multicores = 2, + progress = FALSE + ) + + # Editing cube with invalid files + # (skipping the first line to bypass the cube check and simulate a + # cube containing invalid files) + .fi(cube) <- .fi(cube) |> + dplyr::mutate( + path = ifelse( + dplyr::row_number() > 1, + paste0(path, "_invalid-file"), + path + ) + ) + + + cube_local <- sits_cube_copy( + cube = cube, + output_dir = tempdir(), + progress = FALSE + ) + + expect_equal(nrow(cube_local), 1) + expect_equal(length(sits_timeline(cube_local)), 1) + + # Clean + files <- cube_local[["file_info"]][[1]][["path"]] + unlink(files) +})