Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@
}

if (helpers.isString(storagePath)) {
this.storagePath = () => storagePath;
this.storagePath = async () => storagePath;
} else {
this.storagePath = function () {
let sp = storagePath.apply(self, arguments);
this.storagePath = async function () {
let sp = await storagePath.apply(self, arguments);
if (!helpers.isString(sp)) {
throw new Meteor.Error(400, `[FilesCollection.${self.collectionName}] "storagePath" function must return a String!`);
}
Expand All @@ -342,19 +342,6 @@
};
}

this._debug('[FilesCollection.storagePath] Set to:', this.storagePath({}));

try {
fs.mkdirSync(this.storagePath({}), {
mode: this.parentDirPermissions,
recursive: true
});
} catch (error) {
if (error) {
throw new Meteor.Error(401, `[FilesCollection.${self.collectionName}] Path "${this.storagePath({})}" is not writable!`, error);
}
}

check(this.strict, Boolean);
check(this.permissions, Number);
check(this.storagePath, Function);
Expand Down Expand Up @@ -1056,7 +1043,7 @@
opts.FSName = await this.namingFunction(opts);
}

result.path = `${this.storagePath(result)}${nodePath.sep}${opts.FSName}${extensionWithDot}`;
result.path = `${await this.storagePath(result)}${nodePath.sep}${opts.FSName}${extensionWithDot}`;
result = Object.assign(result, this._dataToSchema(result));

if (this.onBeforeUpload) {
Expand Down Expand Up @@ -1300,7 +1287,7 @@

const {extension, extensionWithDot} = this._getExt(fileName);

opts.path = `${this.storagePath(opts)}${nodePath.sep}${fsName}${extensionWithDot}`;
opts.path = `${await this.storagePath(opts)}${nodePath.sep}${fsName}${extensionWithDot}`;
opts.type = this._getMimeType(opts);
if (!helpers.isObject(opts.meta)) {
opts.meta = {};
Expand Down Expand Up @@ -1419,7 +1406,7 @@
const fileName = (opts.name || opts.fileName) ? (opts.name || opts.fileName) : pathParts[pathParts.length - 1].split('?')[0] || fsName;

const {extension, extensionWithDot} = this._getExt(fileName);
opts.path = `${this.storagePath(opts)}${nodePath.sep}${fsName}${extensionWithDot}`;
opts.path = `${await this.storagePath(opts)}${nodePath.sep}${fsName}${extensionWithDot}`;

// this will be the resolved fileObj
let fileObj;
Expand Down Expand Up @@ -1604,15 +1591,21 @@
try {
_id = await this.collection.insertAsync(result);
} catch (insertErr) {
if (proceedAfterUpload && helpers.isFunction(proceedAfterUpload)) {
await proceedAfterUpload(insertErr);
}
this._debug(`[FilesCollection] [addFileAsync] [insertAsync] Error: ${result.name} -> ${this.collectionName}`, insertErr);
throw new Meteor.Error(insertErr.code, insertErr.message);
}

const fileObj = await this.collection.findOneAsync(_id);

if (proceedAfterUpload === true) {
if (proceedAfterUpload) {
this.onAfterUpload && await this.onAfterUpload.call(this, fileObj);
this.emit('afterUpload', fileObj);
if (helpers.isFunction(proceedAfterUpload)) {
await proceedAfterUpload(null, fileObj);
}
}
this._debug(`[FilesCollection] [addFileAsync]: ${result.name} -> ${this.collectionName}`);
return fileObj;
Expand Down Expand Up @@ -2073,6 +2066,27 @@
break;
}
}

/**
* Ensure storage path exists with proper permissions
*
* @returns {Promise<void>}
* @private
*/
async _ensureStoragePath() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is added but nowhere used. What's its purpose?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That function's content was earlier used inside the constructor to check the existence of the path provided. it was async to check so causing issue while making FilesCollection object. so, moved to separate function. The end user will have flexibility to call this searately if needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its primary purpose is to create directory of not exists.

this._debug('[FilesCollection.storagePath] Set to:', await this.storagePath({}));

try {
fs.mkdirSync(await this.storagePath({}), {
mode: this.parentDirPermissions,
recursive: true
});
} catch (error) {
if (error) {
throw new Meteor.Error(401, `[FilesCollection.${self.collectionName}] Path "${await this.storagePath({})}" is not writable!`, error);

Check failure on line 2086 in server.js

View workflow job for this annotation

GitHub Actions / lint

'self' is not defined
}
}
}
}

export { FilesCollection, WriteStream, helpers };
Loading