-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from besscroft/feature-rss
Feature rss
- Loading branch information
Showing
8 changed files
with
205 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import 'server-only' | ||
import RSS from 'rss' | ||
import { fetchCustomInfo, getRSSImages } from '~/server/db/query' | ||
|
||
export async function GET(request: Request) { | ||
const data = await fetchCustomInfo() | ||
|
||
const url = new URL(request.url); | ||
|
||
const feedId = data?.find((item: any) => item.config_key === 'rss_feed_id')?.config_value?.toString(); | ||
const userId = data?.find((item: any) => item.config_key === 'rss_user_id')?.config_value?.toString(); | ||
|
||
const customElements = feedId && userId | ||
? [ | ||
{ | ||
follow_challenge: [ | ||
{ feedId: feedId }, | ||
{ userId: userId } | ||
] | ||
} | ||
] | ||
: []; | ||
|
||
const feed = new RSS({ | ||
title: data?.find((item: any) => item.config_key === 'custom_title')?.config_value?.toString() || '相册', | ||
generator: 'RSS for Next.js', | ||
feed_url: `${url.origin}/rss.xml`, | ||
site_url: url.origin, | ||
copyright: `© 2024${new Date().getFullYear().toString() === '2024' ? '' : `-${new Date().getFullYear().toString()}`} ${ | ||
data?.find((item: any) => item.config_key === 'custom_author')?.config_value?.toString() || '' | ||
}.`, | ||
pubDate: new Date().toUTCString(), | ||
ttl: 60, | ||
custom_elements: customElements, | ||
}); | ||
|
||
const images = await getRSSImages() | ||
if (Array.isArray(images) && images.length > 0) { | ||
images?.map(item => { | ||
feed.item({ | ||
title: item.title || '图片', | ||
description: ` | ||
<div> | ||
<img src="${item.preview_url || item.url}" alt="${item.detail}" /> | ||
<p>${item.detail}</p> | ||
<a href="${url.origin + (item.album_value === '/' ? '/preview/' : item.album_value + '/preview/') + item.id}" target="_blank">查看图片信息</a> | ||
</div> | ||
`, | ||
url: url.origin + (item.album_value === '/' ? '/preview/' : item.album_value + '/preview/') + item.id, | ||
guid: item.id, | ||
date: item.created_at, | ||
enclosure: { | ||
url: item.preview_url || item.url, | ||
type: 'image/jpeg', | ||
}, | ||
media: { | ||
content: { | ||
url: item.preview_url || item.url, | ||
type: 'image/jpeg', | ||
}, | ||
thumbnail: { | ||
url: item.preview_url || item.url, | ||
}, | ||
}, | ||
}) | ||
}) | ||
} | ||
|
||
return new Response(feed.xml(), { | ||
headers: { | ||
'Content-Type': 'application/xml', | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters