2007-06-23 14:53:39 +00:00
|
|
|
|
// The Antville Project
|
|
|
|
|
// http://code.google.com/p/antville
|
|
|
|
|
//
|
2011-02-23 16:24:32 +00:00
|
|
|
|
// Copyright 2007-2011 by Tobi Schäfer.
|
|
|
|
|
//
|
|
|
|
|
// Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer,
|
|
|
|
|
// Matthias & Michael Platzer, Christoph Lincke.
|
2007-06-23 14:53:39 +00:00
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the ``License'');
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
2014-07-04 15:32:18 +02:00
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2007-06-23 14:53:39 +00:00
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an ``AS IS'' BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
// $Revision$
|
2011-03-15 22:34:38 +00:00
|
|
|
|
// $Author$
|
|
|
|
|
// $Date$
|
2007-06-23 14:53:39 +00:00
|
|
|
|
// $URL$
|
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* @fileOverview Defines the Comment prototype.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-05-24 13:32:40 +00:00
|
|
|
|
markgettext("File");
|
|
|
|
|
markgettext("file");
|
|
|
|
|
|
2007-09-24 22:00:46 +00:00
|
|
|
|
this.handleMetadata("url");
|
|
|
|
|
this.handleMetadata("description");
|
|
|
|
|
this.handleMetadata("contentType");
|
|
|
|
|
this.handleMetadata("contentLength");
|
2008-04-15 15:55:52 +00:00
|
|
|
|
this.handleMetadata("fileName");
|
2007-09-24 22:00:46 +00:00
|
|
|
|
|
2011-03-15 22:34:38 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param {Object} data
|
|
|
|
|
* @param {Site} site
|
|
|
|
|
* @param {User} user
|
|
|
|
|
* @returns {File}
|
|
|
|
|
*/
|
|
|
|
|
File.add = function(data, site, user) {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
site || (site = res.handlers.site);
|
|
|
|
|
user || (user = session.user);
|
|
|
|
|
var file = new File;
|
|
|
|
|
file.site = site;
|
|
|
|
|
file.requests = 0;
|
|
|
|
|
file.creator = file.modifier = user;
|
|
|
|
|
file.created = file.modified = new Date;
|
|
|
|
|
file.update(data);
|
|
|
|
|
site.files.add(file);
|
|
|
|
|
return file;
|
2011-03-15 22:34:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
2014-07-04 15:32:18 +02:00
|
|
|
|
*
|
2009-11-02 16:16:41 +00:00
|
|
|
|
*/
|
2008-05-02 21:33:40 +00:00
|
|
|
|
File.remove = function() {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
if (this.constructor === File) {
|
|
|
|
|
this.getFile().remove();
|
|
|
|
|
this.deleteMetadata();
|
|
|
|
|
this.remove();
|
|
|
|
|
}
|
|
|
|
|
return;
|
2008-05-02 21:33:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
2014-07-04 15:32:18 +02:00
|
|
|
|
*
|
2009-11-02 16:16:41 +00:00
|
|
|
|
* @param {String} name
|
|
|
|
|
*/
|
2008-05-02 18:06:05 +00:00
|
|
|
|
File.getName = function(name) {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
if (name) {
|
|
|
|
|
//return name.replace(/[^\w\d\s._-]/g, String.EMPTY);
|
|
|
|
|
return String(name).trim().replace(/[\/\\:;?+\[\]{}|#"`<>^]/g, String.EMPTY);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2008-05-02 18:06:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-29 08:52:31 +00:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {String } url
|
|
|
|
|
*/
|
|
|
|
|
File.redirectOnUploadError = function(url) {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
if (req.data.helma_upload_error) {
|
|
|
|
|
res.message = gettext("Sorry, the file exceeds the maximum upload limit of {0} kB.",
|
|
|
|
|
formatNumber(app.appsProperties.uploadLimit));
|
|
|
|
|
res.redirect(url);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2011-03-29 08:52:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {String} url
|
|
|
|
|
*/
|
|
|
|
|
File.redirectOnExceededQuota = function(url) {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
if (res.handlers.site.getDiskSpace() < 0) {
|
|
|
|
|
res.message = gettext("Sorry, there is no disk space left. Please try to delete some files or images first.");
|
|
|
|
|
res.redirect(url);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2011-03-29 08:52:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* @name File
|
|
|
|
|
* @constructor
|
|
|
|
|
* @property {Date} created
|
|
|
|
|
* @property {User} creator
|
|
|
|
|
* @property {Metadata} metadata
|
|
|
|
|
* @property {Date} modified
|
|
|
|
|
* @property {User} modifier
|
|
|
|
|
* @property {String} name
|
|
|
|
|
* @property {Number} parent_id
|
|
|
|
|
* @property {String} parent_type
|
|
|
|
|
* @property {String} prototype
|
|
|
|
|
* @property {Number} requests
|
|
|
|
|
* @property {Site} site
|
|
|
|
|
* @extends HopObject
|
|
|
|
|
*/
|
2007-09-24 22:00:46 +00:00
|
|
|
|
File.prototype.constructor = function() {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
return this;
|
2008-04-21 13:57:01 +00:00
|
|
|
|
}
|
2007-06-23 14:53:39 +00:00
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
2014-07-04 15:32:18 +02:00
|
|
|
|
*
|
2009-11-02 16:16:41 +00:00
|
|
|
|
* @param {String} action
|
|
|
|
|
* @return {Boolean}
|
|
|
|
|
*/
|
2007-09-24 22:00:46 +00:00
|
|
|
|
File.prototype.getPermission = function(action) {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
switch (action) {
|
|
|
|
|
case ".":
|
|
|
|
|
case "main":
|
|
|
|
|
return true;
|
|
|
|
|
case "delete":
|
|
|
|
|
case "edit":
|
|
|
|
|
return this._parent.getPermission("main") &&
|
|
|
|
|
this.creator === session.user ||
|
|
|
|
|
Membership.require(Membership.MANAGER) ||
|
|
|
|
|
User.require(User.PRIVILEGED);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2008-04-21 13:57:01 +00:00
|
|
|
|
}
|
2007-06-23 14:53:39 +00:00
|
|
|
|
|
2007-09-24 22:00:46 +00:00
|
|
|
|
File.prototype.main_action = function() {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
if (Membership.require(Membership.SUBSCRIBER) &&
|
|
|
|
|
User.require(User.REGULAR)) {
|
|
|
|
|
this.requests += 1;
|
|
|
|
|
}
|
|
|
|
|
return res.redirect(this.getUrl());
|
2008-04-21 13:57:01 +00:00
|
|
|
|
}
|
2007-06-23 14:53:39 +00:00
|
|
|
|
|
2007-09-24 22:00:46 +00:00
|
|
|
|
File.prototype.edit_action = function() {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
File.redirectOnUploadError(this.href(req.action));
|
2011-03-29 08:52:31 +00:00
|
|
|
|
|
2014-07-04 15:32:18 +02:00
|
|
|
|
if (req.postParams.save) {
|
|
|
|
|
try {
|
|
|
|
|
File.redirectOnExceededQuota(this.href(req.action));
|
|
|
|
|
this.update(req.postParams);
|
|
|
|
|
res.message = gettext("The changes were saved successfully.");
|
|
|
|
|
res.redirect(this._parent.href());
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
res.message = ex;
|
|
|
|
|
app.log(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.data.action = this.href(req.action);
|
|
|
|
|
res.data.title = gettext("Edit File");
|
|
|
|
|
res.data.body = this.renderSkinAsString("$File#edit");
|
|
|
|
|
return this.site.renderSkin("Site#page");
|
2008-04-21 13:57:01 +00:00
|
|
|
|
}
|
2007-06-23 14:53:39 +00:00
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
2014-07-04 15:32:18 +02:00
|
|
|
|
*
|
2009-11-02 16:16:41 +00:00
|
|
|
|
* @param {String} name
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
*/
|
2007-09-24 22:00:46 +00:00
|
|
|
|
File.prototype.getFormValue = function(name) {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
var getOrigin = function(str) {
|
|
|
|
|
var origin = req.postParams.file_origin || self.origin;
|
|
|
|
|
if (origin && origin.contains("://")) {
|
|
|
|
|
return origin;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.isPost()) {
|
|
|
|
|
if (name === "file") {
|
2008-04-15 15:55:52 +00:00
|
|
|
|
return getOrigin();
|
2014-07-04 15:32:18 +02:00
|
|
|
|
}
|
|
|
|
|
return req.postParams[name];
|
|
|
|
|
}
|
|
|
|
|
switch (name) {
|
|
|
|
|
case "file":
|
|
|
|
|
return getOrigin();
|
|
|
|
|
}
|
|
|
|
|
return this[name];
|
2007-09-24 22:00:46 +00:00
|
|
|
|
}
|
2007-06-23 14:53:39 +00:00
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
2014-07-04 15:32:18 +02:00
|
|
|
|
*
|
2009-11-02 16:16:41 +00:00
|
|
|
|
* @param {Object} data
|
|
|
|
|
*/
|
2007-09-24 22:00:46 +00:00
|
|
|
|
File.prototype.update = function(data) {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
if (data.uploadError) {
|
|
|
|
|
app.log(data.uploadError);
|
|
|
|
|
// Looks like the file uploaded has exceeded the upload limit ...
|
|
|
|
|
throw Error(gettext("File size is exceeding the upload limit."));
|
|
|
|
|
}
|
2007-06-23 14:53:39 +00:00
|
|
|
|
|
2014-07-04 15:32:18 +02:00
|
|
|
|
if (!data.file_origin) {
|
|
|
|
|
if (this.isTransient()) {
|
|
|
|
|
throw Error(gettext("There was nothing to upload. Please be sure to choose a file."));
|
|
|
|
|
}
|
|
|
|
|
} else if (data.file_origin !== this.origin) {
|
|
|
|
|
var mime = data.file;
|
|
|
|
|
if (mime.contentLength < 1) {
|
|
|
|
|
mime = getURL(data.file_origin);
|
|
|
|
|
if (!mime) {
|
|
|
|
|
throw Error(gettext("Could not fetch the file from the given URL."));
|
2008-05-02 18:06:05 +00:00
|
|
|
|
}
|
2014-07-04 15:32:18 +02:00
|
|
|
|
}
|
2008-05-02 18:06:05 +00:00
|
|
|
|
|
2014-07-04 15:32:18 +02:00
|
|
|
|
this.origin = data.file_origin;
|
|
|
|
|
var mimeName = mime.normalizeFilename(mime.name);
|
|
|
|
|
this.contentLength = mime.contentLength;
|
|
|
|
|
this.contentType = mime.contentType;
|
2008-04-15 15:55:52 +00:00
|
|
|
|
|
2014-07-04 15:32:18 +02:00
|
|
|
|
if (!this.name) {
|
|
|
|
|
var name = File.getName(data.name) || mimeName.split(".")[0];
|
|
|
|
|
this.name = this.site.files.getAccessName(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make the file persistent before proceeding with writing
|
|
|
|
|
// it to disk (also see Helma bug #607)
|
|
|
|
|
this.isTransient() && this.persist();
|
|
|
|
|
|
|
|
|
|
var extension = mimeName.substr(mimeName.lastIndexOf(".")) || String.EMPTY;
|
|
|
|
|
var fileName = this.name + extension;
|
|
|
|
|
if (fileName !== this.fileName) {
|
|
|
|
|
// Remove existing file if the file name has changed
|
|
|
|
|
this.getFile().remove();
|
|
|
|
|
}
|
|
|
|
|
this.fileName = fileName;
|
|
|
|
|
var file = this.getFile();
|
|
|
|
|
mime.writeToFile(file.getParent(), file.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: one day?
|
|
|
|
|
//this.setTags(data.tags || data.tag_array);
|
|
|
|
|
this.description = data.description;
|
|
|
|
|
this.touch();
|
|
|
|
|
return;
|
2008-04-21 13:57:01 +00:00
|
|
|
|
}
|
2007-06-23 14:53:39 +00:00
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
2014-07-04 15:32:18 +02:00
|
|
|
|
*
|
2009-11-02 16:16:41 +00:00
|
|
|
|
*/
|
2007-09-24 22:00:46 +00:00
|
|
|
|
File.prototype.url_macro = function() {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
return res.write(this.url || this.getUrl());
|
2008-04-21 13:57:01 +00:00
|
|
|
|
}
|
2007-06-23 14:53:39 +00:00
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
2014-07-04 15:32:18 +02:00
|
|
|
|
*
|
2009-11-02 16:16:41 +00:00
|
|
|
|
* @param {Object} param
|
|
|
|
|
*/
|
2007-09-24 22:00:46 +00:00
|
|
|
|
File.prototype.contentLength_macro = function(param) {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
return res.write((this.contentLength / 1024).format("###,###") + " KB");
|
2008-04-21 13:57:01 +00:00
|
|
|
|
}
|
2007-06-23 14:53:39 +00:00
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
2014-07-04 15:32:18 +02:00
|
|
|
|
*
|
2009-11-02 16:16:41 +00:00
|
|
|
|
*/
|
2007-09-24 22:00:46 +00:00
|
|
|
|
File.prototype.getFile = function() {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
var site = this.site || res.handlers.site;
|
|
|
|
|
return site.getStaticFile("files/" + this.fileName);
|
2008-04-21 13:57:01 +00:00
|
|
|
|
}
|
2007-09-21 13:54:30 +00:00
|
|
|
|
|
2009-11-02 16:16:41 +00:00
|
|
|
|
/**
|
2014-07-04 15:32:18 +02:00
|
|
|
|
*
|
2009-11-02 16:16:41 +00:00
|
|
|
|
*/
|
2007-09-24 22:00:46 +00:00
|
|
|
|
File.prototype.getUrl = function() {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
var site = this.site || res.handlers.site;
|
|
|
|
|
return site.getStaticUrl("files/" + this.fileName);
|
2008-04-21 13:57:01 +00:00
|
|
|
|
}
|
2010-02-06 15:13:39 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns {String}
|
|
|
|
|
*/
|
|
|
|
|
File.prototype.getConfirmText = function() {
|
2014-07-04 15:32:18 +02:00
|
|
|
|
return gettext("You are about to delete the file {0}.", this.name);
|
2010-02-06 15:13:39 +00:00
|
|
|
|
}
|