Files
server/Common/sources/storage-fs.js
Alexander.Trofimov 5cc068db6c change license to 2017
2017-01-17 18:02:27 +03:00

1 line
6.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* (c) Copyright Ascensio System SIA 2010-2017
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
* EU, LV-1021.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var utils = require("./utils");
var crypto = require('crypto');
var configStorage = require('config').get('storage');
var cfgBucketName = configStorage.get('bucketName');
var cfgStorageFolderName = configStorage.get('storageFolderName');
var cfgStorageExternalHost = configStorage.get('externalHost');
var configFs = configStorage.get('fs');
var cfgStorageFolderPath = configFs.get('folderPath');
var cfgStorageSecretString = configFs.get('secretString');
function getFilePath(strPath) {
return path.join(cfgStorageFolderPath, strPath);
}
function getOutputPath(strPath) {
return strPath.replace(/\\/g, '/');
}
function removeEmptyParent(strPath, done) {
if (cfgStorageFolderPath.length + 1 >= strPath.length) {
done();
} else {
fs.readdir(strPath, function(err, list) {
if (err) {
//не реагируем на ошибку, потому скорее всего эта папка удалилась в соседнем потоке
done();
} else {
if (list.length > 0) {
done();
} else {
fs.rmdir(strPath, function(err) {
if (err) {
//не реагируем на ошибку, потому скорее всего эта папка удалилась в соседнем потоке
done();
} else {
removeEmptyParent(path.dirname(strPath), function(err) {
done(err);
});
}
});
}
}
});
}
}
exports.getObject = function(strPath) {
return utils.readFile(getFilePath(strPath));
};
exports.putObject = function(strPath, buffer, contentLength) {
return new Promise(function(resolve, reject) {
var fsPath = getFilePath(strPath);
mkdirp(path.dirname(fsPath), function(err) {
if (err) {
reject(err);
} else {
//todo 0666
if (Buffer.isBuffer(buffer)) {
fs.writeFile(fsPath, buffer, function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
} else {
utils.promiseCreateWriteStream(fsPath).then(function(writable) {
buffer.pipe(writable);
}).catch(function(err) {
reject(err);
});
}
}
});
});
};
exports.listObjects = function(strPath) {
return utils.listObjects(getFilePath(strPath)).then(function(values) {
return values.map(function(curvalue) {
return getOutputPath(curvalue.substring(cfgStorageFolderPath.length + 1));
});
});
};
exports.deleteObject = function(strPath) {
return new Promise(function(resolve, reject) {
var fsPath = getFilePath(strPath);
fs.unlink(fsPath, function(err) {
if (err) {
reject(err);
} else {
//resolve();
removeEmptyParent(path.dirname(fsPath), function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
}
});
});
};
exports.deleteObjects = function(strPaths) {
return Promise.all(strPaths.map(exports.deleteObject));
};
exports.getSignedUrl = function(baseUrl, strPath, optUrlExpires, optFilename, opt_type) {
return new Promise(function(resolve, reject) {
//replace '/' with %2f before encodeURIComponent becase nginx determine %2f as '/' and get wrong system path
var userFriendlyName = optFilename ? encodeURIComponent(optFilename.replace(/\//g, "%2f")) : path.basename(strPath);
var uri = '/' + cfgBucketName + '/' + cfgStorageFolderName + '/' + strPath + '/' + userFriendlyName;
var url = (cfgStorageExternalHost ? cfgStorageExternalHost : baseUrl) + uri;
var date = new Date();
var expires = Math.ceil(date.getTime() / 1000) + (optUrlExpires || 604800);
// отключил время жизни т.к. существует сценарий, при котором объект
// получаемый по ссылке запрашивается после того как закончилось время
// его жизни.
var md5 = crypto.createHash('md5').update(/*expires + */uri + cfgStorageSecretString).digest("base64");
md5 = md5.replace(/\+/g, "-");
md5 = md5.replace(/\//g, "_");
url += ('?md5=' + md5 + '&expires=' + expires);
url += '&disposition=' + encodeURIComponent(utils.getContentDisposition(null, null, opt_type));
resolve(utils.changeOnlyOfficeUrl(url, strPath, optFilename));
});
};