Skip to content
Snippets Groups Projects
Select Git revision
2 results Searching

01Uebg_Buergeramt-Di.AssemblyInfoInputs.cache

Blame
  • filechunking.php 5.48 KiB
    <?php
    /**
     * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
     * This file is licensed under the Affero General Public License version 3 or
     * later.
     * See the COPYING-README file.
     */
    
    
    class OC_FileChunking {
    	protected $info;
    	protected $cache;
    
    	static public function decodeName($name) {
    		preg_match('/(?P<name>.*)-chunking-(?P<transferid>\d+)-(?P<chunkcount>\d+)-(?P<index>\d+)/', $name, $matches);
    		return $matches;
    	}
    
    	/**
    	 * @param string[] $info
    	 */
    	public function __construct($info) {
    		$this->info = $info;
    	}
    
    	public function getPrefix() {
    		$name = $this->info['name'];
    		$transferid = $this->info['transferid'];
    
    		return $name.'-chunking-'.$transferid.'-';
    	}
    
    	protected function getCache() {
    		if (!isset($this->cache)) {
    			$this->cache = new \OC\Cache\File();
    		}
    		return $this->cache;
    	}
    
    	/**
    	 * Stores the given $data under the given $key - the number of stored bytes is returned
    	 *
    	 * @param string $index
    	 * @param resource $data
    	 * @return int
    	 */
    	public function store($index, $data) {
    		$cache = $this->getCache();
    		$name = $this->getPrefix().$index;
    		$cache->set($name, $data);
    
    		return $cache->size($name);
    	}
    
    	public function isComplete() {
    		$prefix = $this->getPrefix();
    		$parts = 0;
    		$cache = $this->getCache();
    		for($i=0; $i < $this->info['chunkcount']; $i++) {
    			if ($cache->hasKey($prefix.$i)) {
    				$parts ++;
    			}
    		}
    		return $parts == $this->info['chunkcount'];
    	}
    
    	/**
    	 * Assembles the chunks into the file specified by the path.
    	 * Chunks are deleted afterwards.
    	 *