RUNNb
RUNNb
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
oh ok sorry
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
okok
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
but seeing the disk usage change on the volume it would mean they are going to the volume right?
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
thanks
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
If no futher info is needed, this ticket can be closed 🙂
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
Ok, apparently the image is being saved. I've added a new endpoint to fetch the image content from the volume!
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
The file is being saved now. I've also created an new endpoint to fetch the content from the volume, following the same approach.
options.uploadDir = process.env.RAILWAY_VOLUME_MOUNT_PATH ?
process.env.RAILWAY_VOLUME_MOUNT_PATH : path.join(process.cwd(), "/public/upload_images");

const handler: NextApiHandler = async (req, res) => {
try {
// await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.readdir(process.env.RAILWAY_VOLUME_MOUNT_PATH ?
process.env.RAILWAY_VOLUME_MOUNT_PATH :
path.join(process.cwd() + "/public", "/upload_images"));
} catch (error) {
console.log('directory error: ', error);
// await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.mkdir(
process.env.RAILWAY_VOLUME_MOUNT_PATH ?
process.env.RAILWAY_VOLUME_MOUNT_PATH :
path.join(process.cwd() + "/public", "/upload_images"));
}

try {
const filename = randomUUID();
const { files } = await readFile(req, filename, true);
// @ts-ignore
const extension = files.image[0]?.newFilename.split('.')[1];
res.json({ done: "ok", filename: `${filename}.${extension}` });
} catch (e) {
console.log('error: ', e);
res.json({ done: "nok" });
}
};

export default handler;
options.uploadDir = process.env.RAILWAY_VOLUME_MOUNT_PATH ?
process.env.RAILWAY_VOLUME_MOUNT_PATH : path.join(process.cwd(), "/public/upload_images");

const handler: NextApiHandler = async (req, res) => {
try {
// await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.readdir(process.env.RAILWAY_VOLUME_MOUNT_PATH ?
process.env.RAILWAY_VOLUME_MOUNT_PATH :
path.join(process.cwd() + "/public", "/upload_images"));
} catch (error) {
console.log('directory error: ', error);
// await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.mkdir(
process.env.RAILWAY_VOLUME_MOUNT_PATH ?
process.env.RAILWAY_VOLUME_MOUNT_PATH :
path.join(process.cwd() + "/public", "/upload_images"));
}

try {
const filename = randomUUID();
const { files } = await readFile(req, filename, true);
// @ts-ignore
const extension = files.image[0]?.newFilename.split('.')[1];
res.json({ done: "ok", filename: `${filename}.${extension}` });
} catch (e) {
console.log('error: ', e);
res.json({ done: "nok" });
}
};

export default handler;
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
xD
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
see if it detects the path
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
Ahahah, let me try without the /uploads
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
No description
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
i'll try that
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
I've been searching aswell but nothing about this :/
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
here
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
I've tried to do that. I used the second snippet I've sent
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
No description
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
No description
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
Only when trying to access the image
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
I've tried like this:
import { NextApiHandler, NextApiRequest } from "next";
import formidable from "formidable";
import path from "path";
import fs from "fs/promises";
import {randomUUID} from "crypto";

export const config = {
api: {
bodyParser: false,
},
};

const readFile = (
req: NextApiRequest,
filename: string,
saveLocally?: boolean
): Promise<{ fields: formidable.Fields; files: formidable.Files }> => {
const options: formidable.Options = {};
if (saveLocally) {
options.uploadDir = path.join(process.env.RAILWAY_VOLUME_MOUNT_PATH, "/uploads");
options.filename = (name, ext, path, form) => {
const extension = path.originalFilename?.split(".")[1];
return `${filename}.${extension}`;
};
}
options.maxFileSize = 4000 * 1024 * 1024;
const form = formidable(options);
return new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => {
if (err) reject(err);
resolve({ fields, files });
});
});
};
const handler: NextApiHandler = async (req, res) => {
try {
// await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.readdir(path.join(process.env.RAILWAY_VOLUME_MOUNT_PATH, "/uploads"));
} catch (error) {
// await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.mkdir(path.join(process.env.RAILWAY_VOLUME_MOUNT_PATH, "/uploads"));
}

const filename = randomUUID();
const { files } = await readFile(req, filename, true);
// @ts-ignore
const extension = files.image[0]?.newFilename.split('.')[1];
res.json({ done: "ok", filename: `${filename}.${extension}` });
};
export default handler;
import { NextApiHandler, NextApiRequest } from "next";
import formidable from "formidable";
import path from "path";
import fs from "fs/promises";
import {randomUUID} from "crypto";

export const config = {
api: {
bodyParser: false,
},
};

const readFile = (
req: NextApiRequest,
filename: string,
saveLocally?: boolean
): Promise<{ fields: formidable.Fields; files: formidable.Files }> => {
const options: formidable.Options = {};
if (saveLocally) {
options.uploadDir = path.join(process.env.RAILWAY_VOLUME_MOUNT_PATH, "/uploads");
options.filename = (name, ext, path, form) => {
const extension = path.originalFilename?.split(".")[1];
return `${filename}.${extension}`;
};
}
options.maxFileSize = 4000 * 1024 * 1024;
const form = formidable(options);
return new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => {
if (err) reject(err);
resolve({ fields, files });
});
});
};
const handler: NextApiHandler = async (req, res) => {
try {
// await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.readdir(path.join(process.env.RAILWAY_VOLUME_MOUNT_PATH, "/uploads"));
} catch (error) {
// await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.mkdir(path.join(process.env.RAILWAY_VOLUME_MOUNT_PATH, "/uploads"));
}

const filename = randomUUID();
const { files } = await readFile(req, filename, true);
// @ts-ignore
const extension = files.image[0]?.newFilename.split('.')[1];
res.json({ done: "ok", filename: `${filename}.${extension}` });
};
export default handler;
38 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
I tried that already
38 replies