Skip to content

Commit

Permalink
Merge pull request #1239 from Shelf-nu/adjust-url-shortener-middleware
Browse files Browse the repository at this point in the history
adjustment: logic of handling redirects for url shortener
  • Loading branch information
DonKoko authored Aug 13, 2024
2 parents a3c1990 + 19e6a93 commit 42b5ace
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/components/zxing-scanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const ZXingScanner = ({
* - ^(https?:\/\/[^\/]+\/ matches the protocol, domain, and the initial slash.
* - (?:qr\/)? optionally matches the /qr/ part.
* - ([a-zA-Z0-9]+))$ matches the QR ID which is the last segment of the URL.
* - $ ensures that there are no additional parts after the QR ID.
*/
// Regex to match both old and new QR code structures
const regex = /^(https?:\/\/[^/]+\/(?:qr\/)?([a-zA-Z0-9]+))$/;
Expand Down
18 changes: 6 additions & 12 deletions app/utils/id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@ export function isQrId(id: string): boolean {
const possibleLengths = [DEFAULT_CUID_LENGTH, LEGACY_CUID_LENGTH];
const length = id.length;

// @TODO - temporary disabled number check due to bug and corrupted ids
// /**
// * 1. The string must contain only lowercase letters and digits.
// * 2. The string must start with a lowercase letter.
// * 3. The string must contain at least one digit.
// */
// const regex = /^(?=.*\d)[a-z][0-9a-z]*$/;

/**
* Adjusted criteria:
* 1. The string must contain only lowercase letters.
* 2. The string must start with a lowercase letter.
* The following conditions need to be met for the middleware to redirect to a QR code. In the rest of the cases, it just redirects to app root.
* - The path should NOT include any special characters
* - The path should start with a small letter
* - The path should only have small letters and optional number
* - The path's length should fit within the allowed character lengths(10 for new and 25 for legacy QR codes)
*/
const regex = /^[a-z][0-9a-z]*$/;
const regex = /^[a-z][a-z0-9]*$/;

// Validate the ID against the criteria
if (
Expand Down
11 changes: 11 additions & 0 deletions docs/url-shortener.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
The url shortener functionality allows you to make your QR codes hold less data, thus making them more readable.
The redirection is handled via the `urlShortener` middleware.

## How to use it?

Using the shortener is super easy.

1. Add an env variable with your shortner domain: `URL_SHORTENER="eam.sh"`. You can refer to `.env.example` for further example. The domain should not include the protocol (http/https) or a trailing slash
2. Make sure you point your short domain to your application server
3. Enjoy

## How does it work?

1. When a request is received starting with the shortened url, it gets handled by the `urlShortener` middleware
2. The following conditions need to be met for the middleware to redirect to a QR code. In the rest of the cases, it just redirects to app root.
- The path should NOT include any special characters
- The path should start with a small letter
- The path should only have small letters and optional number
- The path's length should fit within the allowed character lengths(10 for new and 25 for legacy QR codes)
11 changes: 7 additions & 4 deletions server/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export function cache(seconds: number) {
export function urlShortener({ excludePaths }: { excludePaths: string[] }) {
return createMiddleware(async (c, next) => {
const fullPath = c.req.path;

// Remove the URL_SHORTENER part from the beginning of the path
const pathWithoutShortener = fullPath.replace(
`/${process.env.URL_SHORTENER}`,
Expand Down Expand Up @@ -171,9 +172,11 @@ export function urlShortener({ excludePaths }: { excludePaths: string[] }) {
return c.redirect(safeRedirect(redirectUrl), 301);
}

// Handle all other cases
const redirectUrl = `${serverUrl}${pathname}`;
// console.log(`urlShortener middleware: Redirecting to ${redirectUrl}`);
return c.redirect(safeRedirect(redirectUrl), 301);
// console.log(`urlShortener middleware: Redirecting to ${serverUrl}`);
/**
* In all other cases, we just redirect to the app root.
* The URL shortener should only be used for QR codes
* */
return c.redirect(safeRedirect(serverUrl), 301);
});
}

0 comments on commit 42b5ace

Please sign in to comment.