Skip to content
Snippets Groups Projects
Commit cc73722d authored by srosse's avatar srosse
Browse files

Merge OpenOLAT 9.2 to OpenOLAT default branch with 6a1459fc0fa0072c8768a1876bdea40cde0c261e

parents 90745777 6d8b63eb
No related branches found
No related tags found
No related merge requests found
...@@ -19,11 +19,14 @@ ...@@ -19,11 +19,14 @@
*/ */
package org.olat.core.commons.services.webdav.manager; package org.olat.core.commons.services.webdav.manager;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.apache.poi.util.IOUtils;
import org.olat.core.commons.modules.bc.meta.MetaInfo; import org.olat.core.commons.modules.bc.meta.MetaInfo;
import org.olat.core.commons.modules.bc.meta.tagged.MetaTagged; import org.olat.core.commons.modules.bc.meta.tagged.MetaTagged;
import org.olat.core.commons.services.webdav.servlets.WebResource; import org.olat.core.commons.services.webdav.servlets.WebResource;
...@@ -31,13 +34,15 @@ import org.olat.core.commons.services.webdav.servlets.WebResourceRoot; ...@@ -31,13 +34,15 @@ import org.olat.core.commons.services.webdav.servlets.WebResourceRoot;
import org.olat.core.id.Identity; import org.olat.core.id.Identity;
import org.olat.core.logging.OLog; import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing; import org.olat.core.logging.Tracing;
import org.olat.core.util.FileUtils;
import org.olat.core.util.notifications.NotificationsManager; import org.olat.core.util.notifications.NotificationsManager;
import org.olat.core.util.notifications.SubscriptionContext; import org.olat.core.util.notifications.SubscriptionContext;
import org.olat.core.util.vfs.Quota;
import org.olat.core.util.vfs.QuotaExceededException;
import org.olat.core.util.vfs.VFSConstants; import org.olat.core.util.vfs.VFSConstants;
import org.olat.core.util.vfs.VFSContainer; import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSItem; import org.olat.core.util.vfs.VFSItem;
import org.olat.core.util.vfs.VFSLeaf; import org.olat.core.util.vfs.VFSLeaf;
import org.olat.core.util.vfs.VFSManager;
import org.olat.core.util.vfs.VFSStatus; import org.olat.core.util.vfs.VFSStatus;
import org.olat.core.util.vfs.callbacks.VFSSecurityCallback; import org.olat.core.util.vfs.callbacks.VFSSecurityCallback;
import org.olat.core.util.vfs.version.Versionable; import org.olat.core.util.vfs.version.Versionable;
...@@ -52,6 +57,7 @@ import org.olat.core.util.vfs.version.VersionsManager; ...@@ -52,6 +57,7 @@ import org.olat.core.util.vfs.version.VersionsManager;
public class VFSResourceRoot implements WebResourceRoot { public class VFSResourceRoot implements WebResourceRoot {
private static final OLog log = Tracing.createLoggerFor(VFSResourceRoot.class); private static final OLog log = Tracing.createLoggerFor(VFSResourceRoot.class);
private static final int BUFFER_SIZE = 2048;
private final Identity identity; private final Identity identity;
private final VFSContainer base; private final VFSContainer base;
...@@ -160,7 +166,8 @@ public class VFSResourceRoot implements WebResourceRoot { ...@@ -160,7 +166,8 @@ public class VFSResourceRoot implements WebResourceRoot {
} }
@Override @Override
public boolean write(String path, InputStream is, boolean overwrite, WebResource movedFrom) { public boolean write(String path, InputStream is, boolean overwrite, WebResource movedFrom)
throws QuotaExceededException {
VFSLeaf childLeaf; VFSLeaf childLeaf;
VFSItem file = resolveFile(path); VFSItem file = resolveFile(path);
if (file instanceof VFSLeaf) { if (file instanceof VFSLeaf) {
...@@ -201,7 +208,10 @@ public class VFSResourceRoot implements WebResourceRoot { ...@@ -201,7 +208,10 @@ public class VFSResourceRoot implements WebResourceRoot {
} }
try { try {
FileUtils.copy(is, childLeaf.getOutputStream(false)); //FileUtils.copy(is, childLeaf.getOutputStream(false));
copyVFS(childLeaf, is);
} catch (QuotaExceededException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
log.error("", e); log.error("", e);
return false; return false;
...@@ -234,6 +244,57 @@ public class VFSResourceRoot implements WebResourceRoot { ...@@ -234,6 +244,57 @@ public class VFSResourceRoot implements WebResourceRoot {
return true; return true;
} }
private void copyVFS(VFSLeaf file, InputStream is) throws IOException {
// Try to get Quota
long quotaLeft = -1;
boolean withQuotaCheck = false;
VFSContainer parentContainer = file.getParentContainer();
if (parentContainer != null) {
quotaLeft = VFSManager.getQuotaLeftKB(parentContainer);
if (quotaLeft != Quota.UNLIMITED) {
quotaLeft = quotaLeft * 1024; // convert from kB
withQuotaCheck = true;
} else {
withQuotaCheck = false;
}
}
// Open os
OutputStream os = null;
byte buffer[] = new byte[BUFFER_SIZE];
int len = -1;
boolean quotaExceeded = false;
try {
os = file.getOutputStream(false);
while (true) {
len = is.read(buffer);
if (len == -1) break;
if (withQuotaCheck) {
// re-calculate quota and check
quotaLeft = quotaLeft - len;
if (quotaLeft < 0) {
log.info("Quota exceeded: " + file);
quotaExceeded = true;
break;
}
}
os.write(buffer, 0, len);
}
if(quotaExceeded) {
IOUtils.closeQuietly(os);
file.delete();
throw new QuotaExceededException("");
}
} catch (IOException e) {
IOUtils.closeQuietly(os); // close first, in order to be able to delete any reamins of the file
file.delete();
throw e;
} finally {
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(is);
}
}
@Override @Override
public boolean delete(WebResource resource) { public boolean delete(WebResource resource) {
......
...@@ -52,6 +52,7 @@ import org.olat.core.dispatcher.Dispatcher; ...@@ -52,6 +52,7 @@ import org.olat.core.dispatcher.Dispatcher;
import org.olat.core.logging.OLog; import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing; import org.olat.core.logging.Tracing;
import org.olat.core.util.UserSession; import org.olat.core.util.UserSession;
import org.olat.core.util.vfs.QuotaExceededException;
import org.olat.core.util.vfs.VFSItem; import org.olat.core.util.vfs.VFSItem;
import org.olat.core.util.vfs.lock.LockInfo; import org.olat.core.util.vfs.lock.LockInfo;
import org.olat.core.util.vfs.lock.VFSLockManagerImpl; import org.olat.core.util.vfs.lock.VFSLockManagerImpl;
...@@ -873,6 +874,8 @@ public class WebDAVDispatcherImpl ...@@ -873,6 +874,8 @@ public class WebDAVDispatcherImpl
} else { } else {
resp.sendError(HttpServletResponse.SC_CONFLICT); resp.sendError(HttpServletResponse.SC_CONFLICT);
} }
} catch(QuotaExceededException e) {
resp.sendError(HttpServletResponse.SC_CONFLICT);
} finally { } finally {
if (resourceInputStream != null) { if (resourceInputStream != null) {
try { try {
...@@ -1768,10 +1771,15 @@ public class WebDAVDispatcherImpl ...@@ -1768,10 +1771,15 @@ public class WebDAVDispatcherImpl
} }
} else if (sourceResource.isFile()) { } else if (sourceResource.isFile()) {
WebResource movedFrom = moved ? sourceResource : null; WebResource movedFrom = moved ? sourceResource : null;
if (!resources.write(dest, sourceResource.getInputStream(), false, movedFrom)) { try {
errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); if (!resources.write(dest, sourceResource.getInputStream(), false, movedFrom)) {
return false; errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
} return false;
}
} catch (QuotaExceededException e) {
errorList.put(source, new Integer(WebdavStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE));
return false;
}
} else { } else {
errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
return false; return false;
......
...@@ -19,6 +19,7 @@ package org.olat.core.commons.services.webdav.servlets; ...@@ -19,6 +19,7 @@ package org.olat.core.commons.services.webdav.servlets;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collection; import java.util.Collection;
import org.olat.core.util.vfs.QuotaExceededException;
import org.olat.core.util.vfs.VFSItem; import org.olat.core.util.vfs.VFSItem;
...@@ -82,7 +83,8 @@ public interface WebResourceRoot { ...@@ -82,7 +83,8 @@ public interface WebResourceRoot {
* *
* @return <code>true</code> if and only if the new Resource is written * @return <code>true</code> if and only if the new Resource is written
*/ */
boolean write(String path, InputStream is, boolean overwrite, WebResource movedFrom); boolean write(String path, InputStream is, boolean overwrite, WebResource movedFrom)
throws QuotaExceededException;
public boolean delete(WebResource resourceo); public boolean delete(WebResource resourceo);
......
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.core.util.vfs;
import java.io.IOException;
/**
*
* Initial date: 06.01.2014<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class QuotaExceededException extends IOException {
private static final long serialVersionUID = 8875926480691355865L;
public QuotaExceededException(String msg) {
super(msg);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment