Some AI suggestions
Since the API provides the file with EXIF data when accessed directly via the browser, but the EXIF is missing when downloaded via your R script, the issue is likely caused by how httr::GET()
processes the request.
Possible Causes & Fixes:
1. Ensure write_disk()
is Writing Binary Data Correctly
When using httr::GET()
with write_disk()
, the API response should be saved as a binary file. Try explicitly setting mode = "wb"
(write binary) to ensure correct file handling:
library(httr)
for (i in 1:nrow(epicoll)) {
photo_name <- epicoll$X3_TUBE_PHOTO[i]
field_code <- epicoll$X1_FIELD_CODE[i]
reqst <- paste0("https://five.epicollect.net/api/export/media/", proj_slug, "?type=photo&format=entry_original&name=", photo_name)
res <- GET(reqst,
add_headers("Authorization" = paste("Bearer", token)),
write_disk(path = paste0(picture_directory, field_code, ".jpg"), overwrite = TRUE),
config = list(encoding = "gzip")) # Ensure proper encoding handling
}
✅ Why? Some file functions in R default to text mode, which may modify the binary structure.
2. Verify File Integrity After Download
Check if the downloaded file is corrupted or altered by comparing checksums:
system(paste("md5sum", paste0(picture_directory, field_code, ".jpg")))
Then compare it to the MD5 of the file downloaded manually via the browser.
✅ Why? If the checksums don’t match, something is modifying the file during the API download process.
3. Try download.file()
Instead of httr::GET()
If httr::GET()
is altering the file, use download.file()
instead:
for (i in 1:nrow(epicoll)) {
photo_name <- epicoll$X3_TUBE_PHOTO[i]
field_code <- epicoll$X1_FIELD_CODE[i]
reqst <- paste0("https://five.epicollect.net/api/export/media/", proj_slug, "?type=photo&format=entry_original&name=", photo_name)
download.file(reqst, destfile = paste0(picture_directory, field_code, ".jpg"), mode = "wb", quiet = FALSE)
}
✅ Why? download.file()
directly fetches the file and ensures binary integrity.
4. Debug the API Response
To confirm that the API returns a valid image in your script, inspect the response:
res <- GET(reqst, add_headers("Authorization" = paste("Bearer", token)))
# Check content type
print(headers(res)Since the API provides the file with EXIF data when accessed directly via the browser, but the EXIF is missing when downloaded via your R script, the issue is likely caused by how `httr::GET()` processes the request.
### **Possible Causes & Fixes:**
#### **1. Ensure `write_disk()` is Writing Binary Data Correctly**
When using `httr::GET()` with `write_disk()`, the API response should be saved as a binary file. Try explicitly setting `mode = "wb"` (write binary) to ensure correct file handling:
library(httr)
for (i in 1:nrow(epicoll)) {
photo_name ← epicoll$X3_TUBE_PHOTO[i]
field_code ← epicoll$X1_FIELD_CODE[i]
reqst ← paste0(“Epicollect5 - Free and easy-to-use mobile data-gathering platform.”, proj_slug, “?type=photo&format=entry_original&name=”, photo_name)
res ← GET(reqst,
add_headers(“Authorization” = paste(“Bearer”, token)),
write_disk(path = paste0(picture_directory, field_code, “.jpg”), overwrite = TRUE),
config = list(encoding = “gzip”)) # Ensure proper encoding handling
}
✅ **Why?** Some file functions in R default to text mode, which may modify the binary structure.
---
#### **2. Verify File Integrity After Download**
Check if the downloaded file is corrupted or altered by comparing checksums:
system(paste(“md5sum”, paste0(picture_directory, field_code, “.jpg”)))
Then compare it to the MD5 of the file downloaded manually via the browser.
✅ **Why?** If the checksums don’t match, something is modifying the file during the API download process.
---
#### **3. Try `download.file()` Instead of `httr::GET()`**
If `httr::GET()` is altering the file, use `download.file()` instead:
for (i in 1:nrow(epicoll)) {
photo_name ← epicoll$X3_TUBE_PHOTO[i]
field_code ← epicoll$X1_FIELD_CODE[i]
reqst ← paste0(“Epicollect5 - Free and easy-to-use mobile data-gathering platform.”, proj_slug, “?type=photo&format=entry_original&name=”, photo_name)
download.file(reqst, destfile = paste0(picture_directory, field_code, “.jpg”), mode = “wb”, quiet = FALSE)
}
✅ **Why?** `download.file()` directly fetches the file and ensures binary integrity.
---
#### **4. Debug the API Response**
To confirm that the API returns a valid image in your script, inspect the response:
content-type`)
Check response status
print(status_code(res))
If the `content-type` is not `"image/jpeg"`, something might be altering the response.
---
### **Summary**
✅ Try **adding `mode = "wb"` in `write_disk()`**
✅ Compare **file checksums before and after downloading**
✅ **Use `download.file()` instead of `httr::GET()`**
✅ **Inspect API response headers**