feature 1051: ability to add/update a file for an existing photo. For example,

you can add the "high" later. Another example is to update the "web resized"
file (new dimensions is a common example). It also works for thumbnails.
Updating an existing file has no impact on the logical level (list of tags,
list of categories, title, description and so on).

git-svn-id: http://piwigo.org/svn/branches/2.0@4345 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
plegall 2009-11-22 23:38:57 +00:00
parent 6768482bcf
commit d7f0f0f9b2
3 changed files with 174 additions and 3 deletions

View file

@ -942,6 +942,17 @@ function merge_chunks($output_filepath, $original_sum, $type)
{
ws_logfile('[merge_chunks] input parameter $output_filepath : '.$output_filepath);
if (is_file($output_filepath))
{
unlink($output_filepath);
if (is_file($output_filepath))
{
new PwgError(500, '[merge_chunks] error while trying to remove existing '.$output_filepath);
exit();
}
}
$upload_dir = PHPWG_ROOT_PATH.'upload/buffer';
$pattern = '/'.$original_sum.'-'.$type.'/';
$chunks = array();
@ -1038,6 +1049,79 @@ function add_file($file_path, $type, $original_sum, $file_sum)
);
}
function ws_images_addFile($params, &$service)
{
// image_id
// type {thumb, file, high}
// sum
global $conf;
if (!is_admin() || is_adviser() )
{
return new PwgError(401, 'Access denied');
}
$params['image_id'] = (int)$params['image_id'];
if ($params['image_id'] <= 0)
{
return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id");
}
//
// what is the path?
//
$query = '
SELECT
path,
md5sum
FROM '.IMAGES_TABLE.'
WHERE id = '.$params['image_id'].'
;';
list($file_path, $original_sum) = mysql_fetch_row(pwg_query($query));
// TODO only files added with web API can be updated with web API
//
// makes sure directories are there and call the merge_chunks
//
$infos = add_file($file_path, $params['type'], $original_sum, $params['sum']);
//
// update basic metadata from file
//
$update = array();
if ('high' == $params['type'])
{
$update['high_filesize'] = $infos['filesize'];
$update['has_high'] = 'true';
}
if ('file' == $params['type'])
{
$update['filesize'] = $infos['filesize'];
$update['width'] = $infos['width'];
$update['height'] = $infos['height'];
}
// we may have nothing to update at database level, for example with a
// thumbnail update
if (count($update) > 0)
{
$update['id'] = $params['image_id'];
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
mass_updates(
IMAGES_TABLE,
array(
'primary' => array('id'),
'update' => array_diff(array_keys($update), array('id'))
),
array($update)
);
}
}
function ws_images_add($params, &$service)
{
global $conf;

View file

@ -83,11 +83,10 @@ $result = $ua->post(
# print "\n", $ua->cookie_jar->as_string, "\n";
if ($opt{action} eq 'pwg.images.add') {
use MIME::Base64 qw(encode_base64);
use Digest::MD5::File qw/file_md5_hex/;
$form = {};
$form->{method} = 'pwg.images.add';
$form->{method} = $opt{action};
my $original_sum = file_md5_hex($opt{original});
$form->{original_sum} = $original_sum;
@ -145,6 +144,70 @@ if ($opt{action} eq 'pwg.images.add') {
}
}
if ($opt{action} eq 'pwg.images.addFile') {
use Digest::MD5::File qw/file_md5_hex/;
if (not defined $opt{define}{image_id}) {
die '--define image_id=1234 is missing';
}
# which file type are we going to add/update?
my $type = undef;
foreach my $test_type (qw/thumbnail file high/) {
if (defined $opt{$test_type}) {
$type = $test_type;
last;
}
}
if (not defined $type) {
die 'at least one of file/thumbnail/high parameters must be set';
}
my $type_code = typecode_from_typename($type);
send_chunks(
filepath => $opt{$type},
type => $type_code,
original_sum => file_md5_hex($opt{original}),
);
$form = {};
$form->{method} = $opt{action};
$form->{type} = $type_code;
$form->{sum} = file_md5_hex($opt{$type});
foreach my $key (keys %{ $opt{define} }) {
$form->{$key} = $opt{define}{$key};
}
my $response = $ua->post(
$conf{base_url}.'/ws.php?format=json',
$form
);
print "-" x 50, "\n";
printf("response code : %u\n", $response->code);
printf("response message : %s\n", $response->message);
print "-" x 50, "\n";
print "\n";
# use Data::Dumper;
# print Dumper($response->content);
# print Dumper(from_json($response->content));
if ($response->is_success) {
print "upload successful\n";
}
else {
print Dumper($response);
warn 'A problem has occured during upload', "\n";
warn $response->decoded_content, "\n";
die $response->status_line;
}
}
if ($opt{action} eq 'pwg.tags.list') {
use Text::ASCIITable;
@ -333,6 +396,7 @@ sub pwg_ws_get_query {
sub send_chunks {
my %params = @_;
use MIME::Base64 qw(encode_base64);
use File::Slurp;
my $content = read_file($params{filepath});
@ -375,3 +439,15 @@ sub send_chunks {
$chunk_id++;
}
}
sub typecode_from_typename {
my ($typename) = @_;
my $typecode = $typename;
if ('thumbnail' eq $typename) {
$typecode = 'thumb';
}
return $typecode;
}

13
ws.php
View file

@ -183,7 +183,18 @@ function ws_addDefaultMethods( $arr )
),
'POST method only. For admin only.'
);
$service->addMethod(
'pwg.images.addFile',
'ws_images_addFile',
array(
'image_id' => array(),
'type' => array(),
'sum' => array(),
),
'Add or update a file for an existing photo. pwg.images.addChunk must have been called before (maybe several times)'
);
$service->addMethod(
'pwg.images.add',