Skip to content

Commit

Permalink
Merge pull request #4881 from openfoodfacts/move-images-between-servers
Browse files Browse the repository at this point in the history
Move images between servers
  • Loading branch information
stephanegigandet authored Feb 19, 2021
2 parents b7ab00b + d5cfd0b commit 1eef23a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 34 deletions.
26 changes: 19 additions & 7 deletions cgi/product_image_move.pl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
my $code = normalize_code(param('code'));
my $imgids = param('imgids');
my $move_to = param('move_to_override');
if ($move_to ne 'trash') {
if ($move_to =~ /^(off|obf|opf|opff)$/) {
$move_to .= ':' . $code;
}
elsif ($move_to ne 'trash') {
$move_to = normalize_code($move_to);
}
my $copy_data = param('copy_data_override');
Expand Down Expand Up @@ -150,10 +153,13 @@
exit(0);
}

my $new_product_ref = product_exists($move_to_id); # returns 0 if not
my $new_product_ref = retrieve_product($move_to_id);

if (not $new_product_ref) {
$log->info("new product code does not exist yet, creating product", { move_to => $move_to, move_to_id => $move_to_id });
if (defined $new_product_ref) {
$log->debug("new product code already exists", { move_to => $move_to, move_to_id => $move_to_id }) if $log->is_debug();
}
else {
$log->debug("new product code does not exist yet, creating product", { move_to => $move_to, move_to_id => $move_to_id }) if $log->is_debug();
$new_product_ref = init_product($User_id, $Org_id, $move_to, $country);
$new_product_ref->{interface_version_created} = $interface_version;
$new_product_ref->{lc} = $lc;
Expand Down Expand Up @@ -184,11 +190,17 @@

store_product($new_product_ref, "Creating product (moving image from product $code");
}
else {
$log->info("new product code already exists", { move_to => $move_to });
}

$response{url} = product_url($move_to);

# URL on another server?
my $server = server_for_product_id($move_to);
if (defined $server) {
my $url = "https://" . $subdomain . "." . $options{other_servers}{$server}{domain} . $response{url};
$url =~ s/\/([a-z]+):([0-9])/\/$2/;
$response{url} = $url;
}

$response{link} = '<a href="' . $response{url} . '">' . $move_to . '</a>';
}

Expand Down
26 changes: 19 additions & 7 deletions lib/ProductOpener/Images.pm
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,13 @@ sub process_image_upload($$$$$$$) {
if (not "$x") {

# Update the product image data
$log->debug("update the product image data", { imgid => $imgid, product_id => $product_id }) if $log->is_debug();
my $product_ref = retrieve_product($product_id);

if (not defined $product_ref) {
$log->debug("product could not be loaded", { imgid => $imgid, product_id => $product_id }) if $log->is_debug();
}

defined $product_ref->{images} or $product_ref->{images} = {};
$product_ref->{images}{$imgid} = {
uploader => $userid,
Expand All @@ -765,6 +771,8 @@ sub process_image_upload($$$$$$$) {
if ((defined $comment) and ($comment ne '')) {
$store_comment .= ' - ' . $comment;
}

$log->debug("storing product", {product_id => $product_id }) if $log->is_debug();
store_product($product_ref, $store_comment);

# Create a link to the image in /new_images so that it can be batch processed by OCR
Expand Down Expand Up @@ -826,14 +834,14 @@ sub process_image_move($$$$) {
my $ownerid = shift;

# move images only to trash or another valid barcode (number)
if (($move_to ne 'trash') and ($move_to !~ /^\d+$/)) {
if (($move_to ne 'trash') and ($move_to !~ /^((off|obf|opf|opff):)?\d+$/)) {
return "invalid barcode number: $move_to";
}

my $product_id = product_id_for_owner($ownerid, $code);
my $move_to_id = product_id_for_owner($ownerid, $move_to);

$log->debug("process_image_move", { product_id => $product_id, imgids => $imgids, move_to_id => $move_to_id }) if $log->is_debug();
$log->debug("process_image_move - start", { product_id => $product_id, imgids => $imgids, move_to_id => $move_to_id }) if $log->is_debug();

my $path = product_path_from_id($product_id);

Expand All @@ -855,15 +863,18 @@ sub process_image_move($$$$) {
my $new_imgid;
my $debug;

if ($move_to =~ /^\d+$/) {
$ok = process_image_upload($move_to_id, "$www_root/images/products/$path/$imgid.jpg", $product_ref->{images}{$imgid}{uploader}, $product_ref->{images}{$imgid}{uploaded_t}, "image moved from product $code by $User_id -- uploader: $product_ref->{images}{$imgid}{uploader} - time: $product_ref->{images}{$imgid}{uploaded_t}", \$new_imgid, \$debug);
if ($move_to =~ /^((off|obf|opf|opff):)?\d+$/) {
$ok = process_image_upload($move_to_id, "$www_root/images/products/$path/$imgid.jpg", $product_ref->{images}{$imgid}{uploader}, $product_ref->{images}{$imgid}{uploaded_t}, "image moved from product $code on $server_domain by $User_id -- uploader: $product_ref->{images}{$imgid}{uploader} - time: $product_ref->{images}{$imgid}{uploaded_t}", \$new_imgid, \$debug);
if ($ok < 0) {
$log->error("could not move image to other product", { source_path => "$www_root/images/products/$path/$imgid.jpg", old_code => $code, ownerid => $ownerid, user_id => $User_id, result => $ok });
$log->error("could not move image to other product", { source_path => "$www_root/images/products/$path/$imgid.jpg", move_to => $move_to, old_code => $code, ownerid => $ownerid, user_id => $User_id, result => $ok });
}
else {
$log->info("moved image to other product", { source_path => "$www_root/images/products/$path/$imgid.jpg", old_code => $code, ownerid => $ownerid, user_id => $User_id, result => $ok });
$log->info("moved image to other product", { source_path => "$www_root/images/products/$path/$imgid.jpg", move_to => $move_to, old_code => $code, ownerid => $ownerid, user_id => $User_id, result => $ok });
}
}
else {
$log->info("moved image to trash", { source_path => "$www_root/images/products/$path/$imgid.jpg", old_code => $code, ownerid => $ownerid, user_id => $User_id, result => $ok });
}

# Don't delete images to be moved if they weren't moved correctly
if ($ok) {
Expand All @@ -877,7 +888,6 @@ sub process_image_move($$$$) {
$log->info("moving source image to deleted images directory", { source_path => "$www_root/images/products/$path/$imgid.jpg", destination_path => "$data_root/deleted.images/product.$code.$imgid.jpg" });

move("$www_root/images/products/$path/$imgid.jpg", "$data_root/deleted.images/product.$code.$imgid.jpg");
move("$www_root/images/products/$path/$imgid.json", "$data_root/deleted.images/product.$code.$imgid.json");
move("$www_root/images/products/$path/$imgid.$thumb_size.jpg", "$data_root/deleted.images/product.$code.$imgid.$thumb_size.jpg");
move("$www_root/images/products/$path/$imgid.$crop_size.jpg", "$data_root/deleted.images/product.$code.$imgid.$crop_size.jpg");

Expand All @@ -890,6 +900,8 @@ sub process_image_move($$$$) {
}

store_product($product_ref, "Moved images $imgids to $move_to");

$log->debug("process_image_move - end", { product_id => $product_id, imgids => $imgids, move_to_id => $move_to_id }) if $log->is_debug();

return 0;
}
Expand Down
58 changes: 38 additions & 20 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ The product id.
sub product_id_for_owner($$) {

my $ownerid = shift;
my $code = shift;
my $code = shift;

if ((defined $server_options{private_products}) and ($server_options{private_products})) {
if (defined $ownerid) {
Expand All @@ -343,7 +343,7 @@ Returns the server for the product, if it is not on the current server.
=head4 $product_id
Product id of the form [code], [owner-id]/[code], or [server-id]:[code]
Product id of the form [code], [owner-id]/[code], or [server-id]:[code] or [server-id]:[owner-id]/[code]
=head3 Return values
Expand Down Expand Up @@ -501,21 +501,17 @@ sub product_path($) {

sub product_exists($) {

my $id = shift;

my $path = product_path_from_id($id);
if (-e "$data_root/products/$path") {
my $product_id = shift;

my $product_ref = retrieve("$data_root/products/$path/product.sto");
if ((not defined $product_ref) or ($product_ref->{deleted})) {
return 0;
}
else {
return $product_ref;
}
# deprecated, just use retrieve_product()

my $product_ref = retrieve_product($product_id);

if (not defined $product_ref) {
return 0;
}
else {
return 0;
return $product_ref;
}
}

Expand Down Expand Up @@ -590,6 +586,14 @@ sub init_product($$$$) {
my $countryid = shift;

$log->debug("init_product", { userid => $userid, orgid => $orgid, code => $code, countryid => $countryid }) if $log->is_debug();

# We can have a server passed in the code. e.g. obf:43242345
my $server;
if ($code =~ /:/) {
$server = $`;
$code = $';
$log->debug("init_product - found server in code", { userid => $userid, orgid => $orgid, server => $server, code => $code, countryid => $countryid }) if $log->is_debug();
}

my $creator = $userid;

Expand All @@ -605,6 +609,10 @@ sub init_product($$$$) {
creator => $creator,
rev => 0,
};

if (defined $server) {
$product_ref->{server} = $server;
}

if ((defined $server_options{private_products}) and ($server_options{private_products})) {
my $ownerid = get_owner_id($userid, $orgid, $Owner_id);
Expand Down Expand Up @@ -714,18 +722,28 @@ sub retrieve_product($) {
my $path = product_path_from_id($product_id);
my $product_data_root = data_root_for_product_id($product_id);

$log->debug("retrieve_product", { product_id => $product_id, product_data_root => $product_data_root, path => $path } ) if $log->is_debug();
my $full_product_path = "$product_data_root/products/$path/product.sto";

my $product_ref = retrieve("$product_data_root/products/$path/product.sto");
$log->debug("retrieve_product", { product_id => $product_id, product_data_root => $product_data_root, path => $path, full_product_path => $full_product_path } ) if $log->is_debug();

my $product_ref = retrieve($full_product_path);

# If the product is on another server, set the server field so that it will be saved in the other server if we save it
my $server = server_for_product_id($product_id);
if ((defined $product_ref) and (defined $server)) {
$product_ref->{server} = $server;

if (not defined $product_ref) {
$log->debug("retrieve_product - product does not exist", { product_id => $product_id, product_data_root => $product_data_root, path => $path, server => $server } ) if $log->is_debug();
}
else {
if (defined $server) {
$product_ref->{server} = $server;
$log->debug("retrieve_product - product on another server", { product_id => $product_id, product_data_root => $product_data_root, path => $path, server => $server } ) if $log->is_debug();
}

if ((defined $product_ref) and ($product_ref->{deleted})) {
return;
if ($product_ref->{deleted}) {
$log->debug("retrieve_product - deleted product", { product_id => $product_id, product_data_root => $product_data_root, path => $path, server => $server } ) if $log->is_debug();
return;
}
}

return $product_ref;
Expand Down

0 comments on commit 1eef23a

Please sign in to comment.