Changeset 12906 for trunk/include
- Timestamp:
- Jan 17, 2012, 1:11:14 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/ws_functions.inc.php
r12865 r12906 1432 1432 global $conf; 1433 1433 1434 ws_logfile('[ws_images_add_chunk] welcome');1435 1434 // data 1436 1435 // original_sum … … 1570 1569 } 1571 1570 1571 /** 1572 * Function introduced for Piwigo 2.4 and the new "multiple size" 1573 * (derivatives) feature. As we only need the biggest sent photo as 1574 * "original", we remove chunks for smaller sizes. We can't make it earlier 1575 * in ws_images_add_chunk because at this moment we don't know which $type 1576 * will be the biggest (we could remove the thumb, but let's use the same 1577 * algorithm) 1578 */ 1579 function remove_chunks($original_sum, $type) 1580 { 1581 global $conf; 1582 1583 $upload_dir = $conf['upload_dir'].'/buffer'; 1584 $pattern = '/'.$original_sum.'-'.$type.'/'; 1585 $chunks = array(); 1586 1587 if ($handle = opendir($upload_dir)) 1588 { 1589 while (false !== ($file = readdir($handle))) 1590 { 1591 if (preg_match($pattern, $file)) 1592 { 1593 array_push($chunks, $upload_dir.'/'.$file); 1594 } 1595 } 1596 closedir($handle); 1597 } 1598 1599 foreach ($chunks as $chunk) 1600 { 1601 unlink($chunk); 1602 } 1603 } 1604 1572 1605 /* 1573 1606 * The $file_path must be the path of the basic "web sized" photo … … 1578 1611 include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); 1579 1612 1580 $file_path = file_path_for_type($file_path, $type);1581 1582 1613 $upload_dir = dirname($file_path); 1583 1614 if (substr(PHP_OS, 0, 3) == 'WIN') … … 1611 1642 secure_directory($upload_dir); 1612 1643 1613 // merge the thumbnail1644 // merge the file 1614 1645 merge_chunks($file_path, $original_sum, $type); 1615 1646 chmod($file_path, 0644); … … 1634 1665 function ws_images_addFile($params, &$service) 1635 1666 { 1667 ws_logfile(__FUNCTION__.', input : '.var_export($params, true)); 1636 1668 // image_id 1637 1669 // type {thumb, file, high} 1638 // sum 1670 // sum -> not used currently (Piwigo 2.4) 1639 1671 1640 1672 global $conf; … … 1651 1683 1652 1684 // 1653 // what is the path ?1685 // what is the path and other infos about the photo? 1654 1686 // 1655 1687 $query = ' 1656 1688 SELECT 1657 1689 path, 1658 md5sum 1690 file, 1691 md5sum, 1692 width, 1693 height, 1694 filesize 1659 1695 FROM '.IMAGES_TABLE.' 1660 1696 WHERE id = '.$params['image_id'].' 1661 1697 ;'; 1662 list($file_path, $original_sum) = pwg_db_fetch_row(pwg_query($query)); 1663 1664 // TODO only files added with web API can be updated with web API 1665 1666 // 1667 // makes sure directories are there and call the merge_chunks 1668 // 1669 $infos = add_file($file_path, $params['type'], $original_sum, $params['sum']); 1670 1671 // 1672 // update basic metadata from file 1673 // 1674 $update = array(); 1675 1698 $image = pwg_db_fetch_assoc(pwg_query($query)); 1699 1700 if ($image == null) 1701 { 1702 return new PwgError(404, "image_id not found"); 1703 } 1704 1705 // since Piwigo 2.4 and derivatives, we do not take the imported "thumb" 1706 // into account 1707 if ('thumb' == $params['type']) 1708 { 1709 remove_chunks($image['md5sum'], $type); 1710 return true; 1711 } 1712 1713 // since Piwigo 2.4 and derivatives, we only care about the "original" 1714 $original_type = 'file'; 1676 1715 if ('high' == $params['type']) 1677 1716 { 1678 $update['high_filesize'] = $infos['filesize']; 1679 $update['high_width'] = $infos['width']; 1680 $update['high_height'] = $infos['height']; 1681 $update['has_high'] = 'true'; 1682 } 1683 1717 $original_type = 'high'; 1718 } 1719 1720 $file_path = $conf['upload_dir'].'/buffer/'.$image['md5sum'].'-original'; 1721 1722 merge_chunks($file_path, $image['md5sum'], $original_type); 1723 chmod($file_path, 0644); 1724 1725 include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); 1726 1727 // if we receive the "file", we only update the original if the "file" is 1728 // bigger than current original 1684 1729 if ('file' == $params['type']) 1685 1730 { 1686 $update['filesize'] = $infos['filesize']; 1687 $update['width'] = $infos['width']; 1688 $update['height'] = $infos['height']; 1689 } 1690 1691 // we may have nothing to update at database level, for example with a 1692 // thumbnail update 1693 if (count($update) > 0) 1694 { 1695 $update['id'] = $params['image_id']; 1696 1697 include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); 1698 mass_updates( 1699 IMAGES_TABLE, 1700 array( 1701 'primary' => array('id'), 1702 'update' => array_diff(array_keys($update), array('id')) 1703 ), 1704 array($update) 1705 ); 1706 } 1731 $do_update = false; 1732 1733 $infos = pwg_image_infos($file_path); 1734 1735 foreach (array('width', 'height', 'filesize') as $image_info) 1736 { 1737 if ($infos[$image_info] > $image[$image_info]) 1738 { 1739 $do_update = true; 1740 } 1741 } 1742 1743 if (!$do_update) 1744 { 1745 unlink($file_path); 1746 return true; 1747 } 1748 } 1749 1750 $image_id = add_uploaded_file( 1751 $file_path, 1752 $image['file'], 1753 null, 1754 null, 1755 $params['image_id'], 1756 $image['md5sum'] // we force the md5sum to remain the same 1757 ); 1758 1759 include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); 1760 delete_element_derivatives($params['image_id']); 1707 1761 } 1708 1762 … … 1749 1803 } 1750 1804 1751 if ($params['resize']) 1752 { 1753 ws_logfile('[pwg.images.add] resize activated'); 1754 1755 // temporary file path 1756 $type = 'file'; 1757 $file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-'.$type; 1758 1759 merge_chunks($file_path, $params['original_sum'], $type); 1760 chmod($file_path, 0644); 1761 1762 include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); 1763 1764 $image_id = add_uploaded_file( 1765 $file_path, 1766 $params['original_filename'] 1767 ); 1768 1769 // add_uploaded_file doesn't remove the original file in the buffer 1770 // directory if it was not uploaded as $_FILES 1771 unlink($file_path); 1805 // due to the new feature "derivatives" (multiple sizes) introduced for 1806 // Piwigo 2.4, we only take the biggest photos sent on 1807 // pwg.images.addChunk. If "high" is available we use it as "original" 1808 // else we use "file". 1809 remove_chunks($params['original_sum'], 'thumb'); 1810 1811 if (isset($params['high_sum'])) 1812 { 1813 $original_type = 'high'; 1814 remove_chunks($params['original_sum'], 'file'); 1772 1815 } 1773 1816 else 1774 1817 { 1775 // current date 1776 list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();')); 1777 list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); 1778 1779 // upload directory hierarchy 1780 $upload_dir = sprintf( 1781 $conf['upload_dir'].'/%s/%s/%s', 1782 $year, 1783 $month, 1784 $day 1785 ); 1786 1787 // compute file path 1788 $date_string = preg_replace('/[^\d]/', '', $dbnow); 1789 $random_string = substr($params['file_sum'], 0, 8); 1790 $filename_wo_ext = $date_string.'-'.$random_string; 1791 $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg'; 1792 1793 // add files 1794 $file_infos = add_file($file_path, 'file', $params['original_sum'], $params['file_sum']); 1795 $thumb_infos = add_file($file_path, 'thumb', $params['original_sum'], $params['thumbnail_sum']); 1796 1797 if (isset($params['high_sum'])) 1798 { 1799 $high_infos = add_file($file_path, 'high', $params['original_sum'], $params['high_sum']); 1800 } 1801 1802 // database registration 1803 $insert = array( 1804 'file' => !empty($params['original_filename']) ? $params['original_filename'] : $filename_wo_ext.'.jpg', 1805 'date_available' => $dbnow, 1806 'tn_ext' => 'jpg', 1807 'name' => $params['name'], 1808 'path' => $file_path, 1809 'filesize' => $file_infos['filesize'], 1810 'width' => $file_infos['width'], 1811 'height' => $file_infos['height'], 1812 'md5sum' => $params['original_sum'], 1813 'added_by' => $user['id'], 1814 ); 1815 1816 if (isset($params['high_sum'])) 1817 { 1818 $insert['has_high'] = 'true'; 1819 $insert['high_filesize'] = $high_infos['filesize']; 1820 $insert['high_width'] = $high_infos['width']; 1821 $insert['high_height'] = $high_infos['height']; 1822 } 1823 1824 single_insert( 1825 IMAGES_TABLE, 1826 $insert 1827 ); 1828 1829 $image_id = pwg_db_insert_id(IMAGES_TABLE); 1830 1831 // update metadata from the uploaded file (exif/iptc) 1832 require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); 1833 sync_metadata(array($image_id)); 1834 } 1818 $original_type = 'file'; 1819 } 1820 1821 $file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-original'; 1822 1823 merge_chunks($file_path, $params['original_sum'], $original_type); 1824 chmod($file_path, 0644); 1825 1826 include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); 1827 1828 $image_id = add_uploaded_file( 1829 $file_path, 1830 $params['original_filename'], 1831 null, // categories 1832 isset($params['level']) ? $params['level'] : null, 1833 null, // image_id 1834 $params['original_sum'] 1835 ); 1835 1836 1836 1837 $info_columns = array( … … 1838 1839 'author', 1839 1840 'comment', 1840 'level',1841 1841 'date_creation', 1842 1842 ); 1843 1844 $update = array(); 1843 1845 1844 1846 foreach ($info_columns as $key) … … 2339 2341 function ws_images_exist($params, &$service) 2340 2342 { 2343 ws_logfile(__FUNCTION__.' '.var_export($params, true)); 2344 2341 2345 global $conf; 2342 2346 … … 2417 2421 function ws_images_checkFiles($params, &$service) 2418 2422 { 2423 ws_logfile(__FUNCTION__.', input : '.var_export($params, true)); 2424 2419 2425 if (!is_admin()) 2420 2426 { … … 2442 2448 ;'; 2443 2449 $result = pwg_query($query); 2444 if (pwg_db_num_rows($result) == 0) { 2450 if (pwg_db_num_rows($result) == 0) 2451 { 2445 2452 return new PwgError(404, "image_id not found"); 2446 2453 } … … 2449 2456 $ret = array(); 2450 2457 2451 foreach (array('thumb', 'file', 'high') as $type) { 2452 $param_name = $type; 2453 if ('thumb' == $type) { 2454 $param_name = 'thumbnail'; 2455 } 2456 2457 if (isset($params[$param_name.'_sum'])) { 2458 include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); 2459 $type_path = file_path_for_type($path, $type); 2460 if (!is_file($type_path)) { 2461 $ret[$param_name] = 'missing'; 2462 } 2463 else { 2464 if (md5_file($type_path) != $params[$param_name.'_sum']) { 2465 $ret[$param_name] = 'differs'; 2466 } 2467 else { 2468 $ret[$param_name] = 'equals'; 2469 } 2470 } 2471 } 2472 } 2458 if (isset($params['thumbnail_sum'])) 2459 { 2460 // We always say the thumbnail is equal to create no reaction on the 2461 // other side. Since Piwigo 2.4 and derivatives, the thumbnails and web 2462 // sizes are always generated by Piwigo 2463 $ret['thumbnail'] = 'equals'; 2464 } 2465 2466 if (isset($params['high_sum'])) 2467 { 2468 $ret['file'] = 'equals'; 2469 $compare_type = 'high'; 2470 } 2471 elseif (isset($params['file_sum'])) 2472 { 2473 $compare_type = 'file'; 2474 } 2475 2476 if (isset($compare_type)) 2477 { 2478 ws_logfile(__FUNCTION__.', md5_file($path) = '.md5_file($path)); 2479 if (md5_file($path) != $params[$compare_type.'_sum']) 2480 { 2481 $ret[$compare_type] = 'differs'; 2482 } 2483 else 2484 { 2485 $ret[$compare_type] = 'equals'; 2486 } 2487 } 2488 2489 ws_logfile(__FUNCTION__.', output : '.var_export($ret, true)); 2473 2490 2474 2491 return $ret;
Note: See TracChangeset
for help on using the changeset viewer.