Support WRITEABLE_PATH envrionemnt variable

This commit is contained in:
Denis Perov 2022-01-30 15:26:11 +03:00
parent 6a9bc12664
commit 59823a0661
3 changed files with 30 additions and 26 deletions

View file

@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { FilesCollection } from 'meteor/ostrio:files';
import path from 'path';
import { createBucket } from './lib/grid/createBucket';
import { createOnAfterUpload } from './lib/fsHooks/createOnAfterUpload';
import { createInterceptDownload } from './lib/fsHooks/createInterceptDownload';
@ -32,6 +33,12 @@ Attachments = new FilesCollection({
debug: false, // Change to `true` for debugging
collectionName: 'attachments',
allowClientCode: true,
storagePath() {
if (process.env.WRITABLE_PATH) {
return path.join(process.env.WRITABLE_PATH, 'uploads', 'attachments');
}
return path.normalize(`assets/app/uploads/${this.collectionName}`);
},
onAfterUpload: function onAfterUpload(fileRef) {
createOnAfterUpload(attachmentBucket).call(this, fileRef);
// If the attachment doesn't have a source field

View file

@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { FilesCollection } from 'meteor/ostrio:files';
import path from 'path';
import { createBucket } from './lib/grid/createBucket';
import { createOnAfterUpload } from './lib/fsHooks/createOnAfterUpload';
import { createInterceptDownload } from './lib/fsHooks/createInterceptDownload';
@ -14,6 +15,12 @@ Avatars = new FilesCollection({
debug: false, // Change to `true` for debugging
collectionName: 'avatars',
allowClientCode: true,
storagePath() {
if (process.env.WRITABLE_PATH) {
return path.join(process.env.WRITABLE_PATH, 'uploads', 'avatars');
}
return path.normalize(`assets/app/uploads/${this.collectionName}`);;
},
onBeforeUpload(file) {
if (file.size <= 72000 && file.type.startsWith('image/')) {
return true;

View file

@ -1,3 +1,5 @@
import fs from 'fs';
import path from 'path';
import AccountSettings from '../models/accountSettings';
import TableVisibilityModeSettings from '../models/tableVisibilityModeSettings';
import Actions from '../models/actions';
@ -23,8 +25,6 @@ import Triggers from '../models/triggers';
import UnsavedEdits from '../models/unsavedEdits';
import Users from '../models/users';
const fs = require('fs');
// Anytime you change the schema of one of the collection in a non-backward
// compatible way you have to write a migration in this file using the following
// API:
@ -1128,14 +1128,9 @@ Migrations.add('add-card-details-show-lists', () => {
Migrations.add('migrate-attachments-collectionFS-to-ostrioFiles', () => {
AttachmentsOld.find().forEach(function(fileObj) {
//console.log('File: ', fileObj.userId);
// This directory must be writable on server, so a test run first
// We are going to copy the files locally, then move them to S3
const fileName = `./assets/app/uploads/attachments/${
fileObj._id
}-${fileObj.name()}`;
const newFileName = fileObj.name();
const storagePath = Attachments.storagePath({});
const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
// This is "example" variable, change it to the userId that you might be using.
const userId = fileObj.userId;
@ -1145,19 +1140,19 @@ Migrations.add('migrate-attachments-collectionFS-to-ostrioFiles', () => {
const fileId = fileObj._id;
const readStream = fileObj.createReadStream('attachments');
const writeStream = fs.createWriteStream(fileName);
const writeStream = fs.createWriteStream(filePath);
writeStream.on('error', function(err) {
console.log('Writing error: ', err, fileName);
console.log('Writing error: ', err, filePath);
});
// Once we have a file, then upload it to our new data storage
readStream.on('end', () => {
console.log('Ended: ', fileName);
console.log('Ended: ', filePath);
// UserFiles is the new Meteor-Files/FilesCollection collection instance
Attachments.addFile(
fileName,
filePath,
{
fileName: newFileName,
type: fileType,
@ -1186,7 +1181,7 @@ Migrations.add('migrate-attachments-collectionFS-to-ostrioFiles', () => {
});
readStream.on('error', error => {
console.log('Error: ', fileName, error);
console.log('Error: ', filePath, error);
});
readStream.pipe(writeStream);
@ -1195,14 +1190,9 @@ Migrations.add('migrate-attachments-collectionFS-to-ostrioFiles', () => {
Migrations.add('migrate-avatars-collectionFS-to-ostrioFiles', () => {
AvatarsOld.find().forEach(function(fileObj) {
//console.log('File: ', fileObj.userId);
// This directory must be writable on server, so a test run first
// We are going to copy the files locally, then move them to S3
const fileName = `./assets/app/uploads/avatars/${
fileObj._id
}-${fileObj.name()}`;
const newFileName = fileObj.name();
const storagePath = Avatars.storagePath({});
const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
// This is "example" variable, change it to the userId that you might be using.
const userId = fileObj.userId;
@ -1212,19 +1202,19 @@ Migrations.add('migrate-avatars-collectionFS-to-ostrioFiles', () => {
const fileId = fileObj._id;
const readStream = fileObj.createReadStream('avatars');
const writeStream = fs.createWriteStream(fileName);
const writeStream = fs.createWriteStream(filePath);
writeStream.on('error', function(err) {
console.log('Writing error: ', err, fileName);
console.log('Writing error: ', err, filePath);
});
// Once we have a file, then upload it to our new data storage
readStream.on('end', () => {
console.log('Ended: ', fileName);
console.log('Ended: ', filePath);
// UserFiles is the new Meteor-Files/FilesCollection collection instance
Avatars.addFile(
fileName,
filePath,
{
fileName: newFileName,
type: fileType,
@ -1269,7 +1259,7 @@ Migrations.add('migrate-avatars-collectionFS-to-ostrioFiles', () => {
});
readStream.on('error', error => {
console.log('Error: ', fileName, error);
console.log('Error: ', filePath, error);
});
readStream.pipe(writeStream);