Blame view

sources/lib/private/connector/sabre/quotaplugin.php 2.67 KB
03e52840d   Kload   Init
1
  <?php
6d9380f96   Cédric Dupont   Update sources OC...
2
  use Sabre\DAV\URLUtil;
03e52840d   Kload   Init
3
4
5
6
7
8
9
10
  
  /**
   * This plugin check user quota and deny creating files when they exceeds the quota.
   *
   * @author Sergio Cambra
   * @copyright Copyright (C) 2012 entreCables S.L. All rights reserved.
   * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
   */
6d9380f96   Cédric Dupont   Update sources OC...
11
12
13
14
15
16
  class OC_Connector_Sabre_QuotaPlugin extends \Sabre\DAV\ServerPlugin {
  
  	/**
  	 * @var \OC\Files\View
  	 */
  	private $view;
03e52840d   Kload   Init
17
18
19
20
  
  	/**
  	 * Reference to main server object
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
21
  	 * @var \Sabre\DAV\Server
03e52840d   Kload   Init
22
23
24
25
  	 */
  	private $server;
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
26
  	 * @param \OC\Files\View $view
03e52840d   Kload   Init
27
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
28
29
30
  	public function __construct($view) {
  		$this->view = $view;
  	}
03e52840d   Kload   Init
31
32
33
34
  
  	/**
  	 * This initializes the plugin.
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
35
  	 * This function is called by \Sabre\DAV\Server, after
03e52840d   Kload   Init
36
37
38
39
  	 * addPlugin is called.
  	 *
  	 * This method should set up the requires event subscriptions.
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
40
  	 * @param \Sabre\DAV\Server $server
03e52840d   Kload   Init
41
42
  	 * @return void
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
43
  	public function initialize(\Sabre\DAV\Server $server) {
03e52840d   Kload   Init
44
45
46
47
48
49
50
51
52
53
  
  		$this->server = $server;
  
  		$server->subscribeEvent('beforeWriteContent', array($this, 'checkQuota'), 10);
  		$server->subscribeEvent('beforeCreateFile', array($this, 'checkQuota'), 10);
  	}
  
  	/**
  	 * This method is called before any HTTP method and validates there is enough free space to store the file
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
54
55
56
  	 * @param string $uri
  	 * @param null $data
  	 * @throws \Sabre\DAV\Exception\InsufficientStorage
03e52840d   Kload   Init
57
58
59
60
61
  	 * @return bool
  	 */
  	public function checkQuota($uri, $data = null) {
  		$length = $this->getLength();
  		if ($length) {
6d9380f96   Cédric Dupont   Update sources OC...
62
63
  			if (substr($uri, 0, 1) !== '/') {
  				$uri = '/' . $uri;
03e52840d   Kload   Init
64
  			}
6d9380f96   Cédric Dupont   Update sources OC...
65
  			list($parentUri, $newName) = URLUtil::splitPath($uri);
837968727   Kload   [enh] Upgrade to ...
66
67
68
69
  			$req = $this->server->httpRequest;
  			if ($req->getHeader('OC-Chunked')) {
  				$info = OC_FileChunking::decodeName($newName);
  				$chunkHandler = new OC_FileChunking($info);
6d9380f96   Cédric Dupont   Update sources OC...
70
  				// subtract the already uploaded size to see whether
837968727   Kload   [enh] Upgrade to ...
71
72
73
  				// there is still enough space for the remaining chunks
  				$length -= $chunkHandler->getCurrentSize();
  			}
03e52840d   Kload   Init
74
  			$freeSpace = $this->getFreeSpace($parentUri);
31b7f2792   Kload   Upgrade to ownclo...
75
  			if ($freeSpace !== \OC\Files\SPACE_UNKNOWN && $length > $freeSpace) {
837968727   Kload   [enh] Upgrade to ...
76
77
78
  				if (isset($chunkHandler)) {
  					$chunkHandler->cleanup();
  				}
6d9380f96   Cédric Dupont   Update sources OC...
79
  				throw new \Sabre\DAV\Exception\InsufficientStorage();
03e52840d   Kload   Init
80
81
82
83
  			}
  		}
  		return true;
  	}
6d9380f96   Cédric Dupont   Update sources OC...
84
  	public function getLength() {
03e52840d   Kload   Init
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  		$req = $this->server->httpRequest;
  		$length = $req->getHeader('X-Expected-Entity-Length');
  		if (!$length) {
  			$length = $req->getHeader('Content-Length');
  		}
  
  		$ocLength = $req->getHeader('OC-Total-Length');
  		if ($length && $ocLength) {
  			return max($length, $ocLength);
  		}
  
  		return $length;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
100
  	 * @param string $parentUri
03e52840d   Kload   Init
101
102
  	 * @return mixed
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
103
104
  	public function getFreeSpace($parentUri) {
  		$freeSpace = $this->view->free_space($parentUri);
03e52840d   Kload   Init
105
106
107
  		return $freeSpace;
  	}
  }