Skip to content

Commit

Permalink
Add gphoto2: storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
weetmuts committed Jul 31, 2024
1 parent 17186ad commit 0493bf1
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
125 changes: 125 additions & 0 deletions src/beak_cameramedia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "media.h"
#include "storagetool.h"
#include "storage_aftmtp.h"
#include "storage_gphoto2.h"
#include "system.h"
#include "util.h"

Expand Down Expand Up @@ -185,6 +186,125 @@ RC import_aft_mtp_cli(Settings *settings,
return rc;
}

RC import_gphoto2(Settings *settings,
Monitor *monitor,
System *sys,
FileSystem *local_fs,
BeakImplementation *bi)
{
assert(settings->from.storage->type == GPhoto2Storage);
//Path *home = Path::lookup(getenv("HOME"));
//Path *cache = home->append(".cache/beak/temp-beak-media-import");

local_fs->allowAccessTimeUpdates();

assert(settings->to.type == ArgDir);
Path *destination = settings->to.dir;

// The directory name where we store the media files.
string archive_name = destination->name()->str();

// Establish access to the phone/camera and get the device name.
string device_name = gphoto2EstablishAccess(sys);

info(CAMERA, "Importing media from %s into %s\n", device_name.c_str(), archive_name.c_str());

return RC::OK;
/*
unique_ptr<ProgressStatistics> progress = monitor->newProgressStatistics(buildJobName("listing", settings), "list");
map<Path*,FileStat> files;
// Just list the files in the android phone, The crappy mtp protocol is soooo slow just listing the files.
// Anyway we do this once (hopefully unless there is a random disconnect) then we can check size and date
// for if we think we have imported this file already. I.e. no need to download slowly over mtp to do
// the full processing.
for (;;)
{
RC rc = aftmtpListFiles(settings->from.storage,
&files,
sys,
progress.get());
if (rc.isOk()) break;
// The mtp link crashed already in the first transfer.... Blech.
aftmtpReEstablishAccess(sys, true);
}
UI::output("Found ... new files not yet in %s", archive_name.c_str());
vector<pair<Path*,FileStat>> potential_files_to_copy;
for (auto &p : files)
{
bool already_exists = check_if_already_exists(p.first, p.second, local_fs, destination);
if (!already_exists)
{
potential_files_to_copy.push_back(p);
debug(CAMERA, "potential download %s\n", p.first->c_str());
UI::clearLine();
UI::output("Found %zu new files not yet in %s", potential_files_to_copy.size(), archive_name.c_str());
}
}
UI::clearLine();
if (potential_files_to_copy.size() == 0)
{
UI::clearLine();
info(CAMERA, "All files imported into %s already.\n", archive_name.c_str());
return RC::OK;
}
// We have some potential files that we do not think have been imported yet.
// Lets download them into a temporary dir from which we can import them properly.
local_fs->mkDirpWriteable(cache);
// Downloading from the phone/camera using mtp can take some time, lets track the progress.
progress = monitor->newProgressStatistics(buildJobName("copying", settings), "copy");
vector<Path*> files_to_copy;
for (auto &p : potential_files_to_copy)
{
// Check if the file has already been copied to the temporary dir.
// Remember that the usb connection to the phone can break at any time.
// We want to pickup where we left off.
Path *dest_file = p.first->prepend(cache);
addWork(progress.get(), p.first, p.second, local_fs, dest_file, &files_to_copy);
// The file is added (or not) to files_to_copy.
}
size_t already_in_cache = potential_files_to_copy.size()-files_to_copy.size();
size_t already_imported = files.size()-files_to_copy.size();
info(CAMERA, "Downloading %zu files (%zu already in cache and %zu already fully imported into %s).\n",
files_to_copy.size(),
already_in_cache,
already_imported,
archive_name.c_str());
progress->startDisplayOfProgress();
RC rc = aftmtpFetchFiles(settings->from.storage,
&files_to_copy,
cache,
sys,
local_fs,
progress.get());
progress->finishProgress();
// Now import the cache into the real archive.
settings->from.type = ArgDir;
settings->from.dir = cache;
settings->to.type = ArgStorage;
Storage st {};
settings->to.storage = &st;
settings->to.storage->storage_location = destination;
settings->to.storage->type = FileSystemStorage;
rc = bi->importMedia(settings, monitor);
return rc;
*/
}

RC BeakImplementation::cameraMedia(Settings *settings, Monitor *monitor)
{
assert(settings->from.type == ArgStorage);
Expand All @@ -194,5 +314,10 @@ RC BeakImplementation::cameraMedia(Settings *settings, Monitor *monitor)
return import_aft_mtp_cli(settings, monitor, sys_, local_fs_, this);
}

if (settings->from.storage->type == GPhoto2Storage)
{
return import_gphoto2(settings, monitor, sys_, local_fs_, this);
}

return RC::ERR;
}
18 changes: 18 additions & 0 deletions src/configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class ConfigurationImplementation : public Configuration
bool isRCloneStorage(Path *storage_location, string *type = NULL);
bool isRSyncStorage(Path *storage_location);
bool isAftMtpStorage(Path *storage_location);
bool isGPhoto2Storage(Path *storage_location);

// Map rule name to rule.
map<string,Rule> rules_;
Expand Down Expand Up @@ -1133,6 +1134,17 @@ bool ConfigurationImplementation::isAftMtpStorage(Path *storage_location)
return prefix == "aftmtp:";
}

bool ConfigurationImplementation::isGPhoto2Storage(Path *storage_location)
{
string arg = storage_location->str();
auto colon = arg.find(':');
if (colon == string::npos) return false;

string prefix = arg.substr(0,colon+1);

return prefix == "gphoto2:";
}

// Storage created on the fly depending on the command line arguments.
Storage a_storage;

Expand Down Expand Up @@ -1166,6 +1178,12 @@ Storage *ConfigurationImplementation::findStorageFrom(Path *storage_location, Co
a_storage.storage_location = storage_location;
return &a_storage;
}
else if (isGPhoto2Storage(storage_location))
{
a_storage.type = GPhoto2Storage;
a_storage.storage_location = storage_location;
return &a_storage;
}
else
{
Path *rp = storage_location->realpath();
Expand Down

0 comments on commit 0493bf1

Please sign in to comment.