Ignore:
Timestamp:
May 27, 2014, 4:11:23 PM (10 years ago)
Author:
plg
Message:

feature 2616: HTML5 upload (with plupload 2.1.2). First basic implementation. Needs customization.

Chunked upload + Drag & drop (no more Flash)

use a new specific API method pwg.images.upload

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/ws_functions/pwg.images.php

    r28087 r28545  
    12451245/**
    12461246 * API method
     1247 * Adds a image (simple way)
     1248 * @param mixed[] $params
     1249 *    @option int[] category
     1250 *    @option string name (optional)
     1251 *    @option string author (optional)
     1252 *    @option string comment (optional)
     1253 *    @option int level
     1254 *    @option string|string[] tags
     1255 *    @option int image_id (optional)
     1256 */
     1257function ws_images_upload($params, $service)
     1258{
     1259  global $conf;
     1260
     1261  if (get_pwg_token() != $params['pwg_token'])
     1262  {
     1263    return new PwgError(403, 'Invalid security token');
     1264  }
     1265
     1266  // usleep(100000);
     1267
     1268  // if (!isset($_FILES['image']))
     1269  // {
     1270  //   return new PwgError(405, 'The image (file) is missing');
     1271  // }
     1272 
     1273  // file_put_contents('/tmp/plupload.log', "[".date('c')."] ".__FUNCTION__."\n\n", FILE_APPEND);
     1274  // file_put_contents('/tmp/plupload.log', '$_FILES = '.var_export($_FILES, true)."\n", FILE_APPEND);
     1275  // file_put_contents('/tmp/plupload.log', '$_POST = '.var_export($_POST, true)."\n", FILE_APPEND);
     1276
     1277  $upload_dir = $conf['upload_dir'].'/buffer';
     1278
     1279  // create the upload directory tree if not exists
     1280  if (!mkgetdir($upload_dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR))
     1281  {
     1282    return new PwgError(500, 'error during buffer directory creation');
     1283  }
     1284
     1285  // Get a file name
     1286  if (isset($_REQUEST["name"]))
     1287  {
     1288    $fileName = $_REQUEST["name"];
     1289  }
     1290  elseif (!empty($_FILES))
     1291  {
     1292    $fileName = $_FILES["file"]["name"];
     1293  }
     1294  else
     1295  {
     1296    $fileName = uniqid("file_");
     1297  }
     1298
     1299  $filePath = $upload_dir.DIRECTORY_SEPARATOR.$fileName;
     1300
     1301  // Chunking might be enabled
     1302  $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
     1303  $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
     1304
     1305  file_put_contents('/tmp/plupload.log', "[".date('c')."] ".__FUNCTION__.', '.$fileName.' '.($chunk+1).'/'.$chunks."\n", FILE_APPEND);
     1306
     1307  single_insert(
     1308    'plupload',
     1309    array(
     1310      'received_on' => date('c'),
     1311      'filename' => $fileName,
     1312      'chunk' => $chunk+1,
     1313      'chunks' => $chunks,
     1314      )
     1315    );
     1316
     1317
     1318  // Open temp file
     1319  if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb"))
     1320  {
     1321    die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
     1322  }
     1323
     1324  if (!empty($_FILES))
     1325  {
     1326    if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"]))
     1327    {
     1328      die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
     1329    }
     1330
     1331    // Read binary input stream and append it to temp file
     1332    if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb"))
     1333    {
     1334      die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
     1335    }
     1336  }
     1337  else
     1338  {
     1339    if (!$in = @fopen("php://input", "rb"))
     1340    {
     1341      die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
     1342    }
     1343  }
     1344
     1345  while ($buff = fread($in, 4096))
     1346  {
     1347    fwrite($out, $buff);
     1348  }
     1349
     1350  @fclose($out);
     1351  @fclose($in);
     1352
     1353  // Check if file has been uploaded
     1354  if (!$chunks || $chunk == $chunks - 1)
     1355  {
     1356    // Strip the temp .part suffix off
     1357    rename("{$filePath}.part", $filePath);
     1358 
     1359    include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
     1360   
     1361    $image_id = add_uploaded_file(
     1362      $filePath,
     1363      $params['name'],
     1364      $params['category'],
     1365      $params['level'],
     1366      null // image_id = not provided, this is a new photo
     1367      );
     1368   
     1369    $query = '
     1370SELECT
     1371    id,
     1372    path
     1373  FROM '.IMAGES_TABLE.'
     1374  WHERE id = '.$image_id.'
     1375;';
     1376    $image_infos = pwg_db_fetch_assoc(pwg_query($query));
     1377
     1378    return array(
     1379      'image_id' => $image_id,
     1380      'src' => DerivativeImage::thumb_url($image_infos),
     1381      );
     1382  }
     1383}
     1384
     1385/**
     1386 * API method
    12471387 * Check if an image exists by it's name or md5 sum
    12481388 * @param mixed[] $params
Note: See TracChangeset for help on using the changeset viewer.