Skip to content

Commit

Permalink
Add shrtn auth support
Browse files Browse the repository at this point in the history
  • Loading branch information
edfletcher committed Nov 17, 2024
1 parent 3d7886d commit bb69ab6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
7 changes: 6 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ const _config = {
defaultFontSizePt: 14
},
// the root URL of a running instance of https://github.com/rpj/shrtn which will be used to shorten attachment links
shrtnHost: null
shrtnHost: null,
// if the above is non-null and your shrtn is configured with authorization enabled, enter your user credentials here
shrtnCreds: {
user: null,
pass: null
}
},

ipinfo: {
Expand Down
23 changes: 18 additions & 5 deletions http.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,27 @@ async function createShrtned (fromUrl) {
return null;
}

const { redirect } = await (await fetch(config.http.shrtnHost + '/add', {
const headers = {
Accept: 'application/json'
};

if (config.http.shrtnCreds?.user && config.http.shrtnCreds?.pass) {
const { user, pass } = config.http.shrtnCreds;
headers.Authorization = `Basic ${Buffer.from(`${user}:${pass}`, 'utf-8').toString('base64')}`;
}

const response = await fetch(config.http.shrtnHost + '/add', {
method: 'POST',
body: fromUrl,
headers: {
Accept: 'application/json'
}
})).json();
headers
});

if (!response.ok) {
console.error(`Shrtn request failed: ${response.status} "${response.statusText}"`);
return null;
}

const { redirect } = await response.json();
return `${config.http.shrtnHost}/${redirect}`;
}

Expand Down

0 comments on commit bb69ab6

Please sign in to comment.