Hey @idc-dev_idc-dev,
I hope all is well with you!
I tried to look around and figure out a solution for you. I will explain the easiest and most straight forward solution that I could think of.
What we are going to do is to extend the find and findOne controllers of the upload plugin (on the API and admin side), to replace the URL with your host domain. You can store your UPLOAD_URL in a env variable and hard code a default one.
First create the following file:
project/src/extension/upload/strapi-server.js
with the following code (which is compiled from the original upload plugin):
// Utilities and necessary functions for parsing your data
const utils = require('@strapi/utils');
const { sanitize } = utils;
const ACTIONS = {
read: 'plugin::upload.read',
readSettings: 'plugin::upload.settings.read',
create: 'plugin::upload.assets.create',
update: 'plugin::upload.assets.update',
download: 'plugin::upload.assets.download',
copyLink: 'plugin::upload.assets.copy-link',
}; const fileModel = 'plugin::upload.file';
const getService = name => {
return strapi.plugin('upload').service(name);
};
const sanitizeOutput = (data, ctx) => {
const schema = strapi.getModel('plugin::upload.file');
const { auth } = ctx.state;
return sanitize.contentAPI.output(data, schema, { auth });
};
// Parser function
function prefixUrl(data, url = process.env['UPLOAD_URL'] || 'https://google.com/') {
var formats = data.formats.thumbnail ? {
thumbnail: data.formats.thumbnail.url.replace('/', url),
large: data.formats.large.url.replace('/', url),
medium: data.formats.medium.url.replace('/', url),
small: data.formats.small.url.replace('/', url),
} : data.formats
return {
...data, url: data.url.replace('/', url), formats
}
}
module.exports = (plugin) => {
// Client Side Requests
plugin.controllers['content-api'].find = async ctx => {
const files = await getService('upload').findMany(ctx.query).then(items => items.map(each => prefixUrl(each)));
console.log(files)
ctx.body = await sanitizeOutput(files, ctx)
}
plugin.controllers['content-api'].findOne = async ctx => {
const {
params: { id },
} = ctx;
const file = await getService('upload').findOne(id);
if (!file) {
return ctx.notFound('file.notFound');
}
ctx.body = await sanitizeOutput(file, ctx).then(item => prefixUrl(item));
}
// Admin Side Requests
plugin.controllers['admin-api'].find = async ctx => {
const {
state: { userAbility },
} = ctx;
const pm = strapi.admin.services.permission.createPermissionsManager({
ability: userAbility,
action: ACTIONS.read,
model: fileModel,
});
if (!pm.isAllowed) {
return ctx.forbidden();
}
const query = pm.addPermissionsQueryTo(ctx.query);
const { results, pagination } = await getService('upload').findPage(query);
const sanitizedResults = await pm.sanitizeOutput(results).then(items => items.map(each => prefixUrl(each)));
return { results: sanitizedResults, pagination };
}
plugin.controllers['admin-api'].findOne = async ctx => {
const {
state: { userAbility },
params: { id },
} = ctx;
const { pm, file } = await findEntityAndCheckPermissions(
userAbility,
ACTIONS.read,
fileModel,
id
);
ctx.body = await pm.sanitizeOutput(file).then(item => prefixUrl(item));
}
return plugin;
};
and following that, your API responses should be modified:
as well as your admin panel URLs:
This is a bit of a unique use case, so please let me know if that helped!
All the best