A modern image optimization and delivery service built with SvelteKit. Upload once, serve everywhere with automatic optimization and lightning-fast delivery.
git clone https://github.com/ifsvivek/ImageCDN
cd ImageCDN
npm install
Create a .env
file in the root directory with the following variables:
ACCESS_TOKEN="your_github_personal_access_token"
POSTGRES_URL="your_neon_postgres_connection_string"
JWT_SECRET="your_random_secret_key"
Create a new repository named CDN
in your GitHub account.
Generate a personal access token with repo
permissions.
Update the following lines in src/routes/api/upload/+server.js
with your GitHub username:
await octokit.repos.createOrUpdateFileContents({
owner: 'your-github-username',
repo: 'CDN',
path,
message: `Upload ${path}`,
content: resizedImage.toString('base64'),
branch: 'main'
});
const fileData = await octokit.repos.getContent({
owner: 'your-github-username',
repo: 'CDN',
path
});
await octokit.repos.deleteFile({
owner: 'your-github-username',
repo: 'CDN',
path,
message: `Delete ${path}`,
sha: fileData.data.sha,
branch: 'main'
});
return json({
success: true,
urls: sizes.map(
(size) =>
`https://cdn.your-github-username.io/${userId}/${timestamp}/image_${size}.${format}`
)
});
Create a free database on Neon.
Execute the following SQL commands to create the required tables:
CREATE TABLE user_table (
user_id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
user_name VARCHAR(255) NOT NULL
);
CREATE TABLE user_images (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES user_table(user_id),
foldername VARCHAR(255) NOT NULL,
format VARCHAR(10) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE api_keys (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES user_table(user_id),
api_key VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE api_usage (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES user_table(user_id),
api_key VARCHAR(255) REFERENCES api_keys(api_key),
used_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Start the development server:
npm run dev
Build the project:
npm run build
Preview the production build:
npm run preview
After uploading images, please allow up to 20 minutes for processing and CDN availability. This ensures optimal compression and format conversion.
ACCESS_TOKEN
: GitHub personal access token with repo
permissionsPOSTGRES_URL
: Neon PostgreSQL connection stringJWT_SECRET
: Random string used for JWT token generationImages will be available at:
https://cdn.yourdomain.com/{user_id}/{timestamp}/image_{size}.{format}
Where:
size
: 256, 512, 1024, or 2048format
: webp, jpg, or pngThis project is licensed under the MIT License. See the LICENSE file for details.