Skip to content

Commit

Permalink
feat: check resolution width and height
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 24, 2022
1 parent 95028c7 commit 824d42b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-novelai",
"description": "Generate images by NovelAI",
"version": "1.11.6",
"version": "1.11.8",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export const Config = Schema.intersect([
}),
Schema.object({
type: Schema.const('login'),
email: Schema.string().description('用户名。').required(),
password: Schema.string().description('密码。').role('secret').required(),
email: Schema.string().description('账号邮箱。').required(),
password: Schema.string().description('账号密码。').role('secret').required(),
}),
]),
Schema.object({
Expand Down
43 changes: 24 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@ export function apply(ctx: Context, config: Config) {
}
}

const checkLength = (length: number) => length % 64 === 0 && length > 0

const resolution = (source: string, session: Session<'authority'>): Size => {
if (source in orientMap) return orientMap[source]
if (restricted(session)) throw new Error()
const cap = source.match(/^(\d+)[x×](\d+)$/)
if (!cap) throw new Error()
return { width: +cap[1], height: +cap[2] }
const width = +cap[1], height = +cap[2]
if (!checkLength(width) || !checkLength(height) || width + height > 1280) {
throw new Error('commands.novelai.messages.invalid-resolution')
}
return { width, height }
}

const cmd = ctx.command('novelai <prompts:text>')
Expand Down Expand Up @@ -205,17 +211,27 @@ export function apply(ctx: Context, config: Config) {
globalTasks.delete(id)
}

function getPostData() {
const path = (() => {
switch (config.type) {
case 'sd-webui':
return parameters.image ? '/sdapi/v1/img2img' : '/sdapi/v1/txt2img'
case 'naifu':
return '/generate-stream'
default:
return '/ai/generate-image'
}
})()

const data = (() => {
if (config.type !== 'sd-webui') {
parameters.sampler = sampler.sd2nai(options.sampler)
return config.type === 'naifu'
? parameters
: { model, input: prompt, parameters: omit(parameters, ['prompt']) }
if (config.type === 'naifu') return parameters
return { model, input: prompt, parameters: omit(parameters, ['prompt']) }
}

return {
sampler_index: sampler.sd[options.sampler],
init_images: parameters.image ? [parameters.image] : undefined,
init_images: parameters.image && [parameters.image],
...project(parameters, {
prompt: 'prompt',
batch_size: 'n_samples',
Expand All @@ -225,30 +241,19 @@ export function apply(ctx: Context, config: Config) {
steps: 'steps',
width: 'width',
height: 'height',
// img2img parameters
denoising_strength: 'strength',
}),
}
}

const path = (() => {
switch (config.type) {
case 'sd-webui':
return parameters.image ? '/sdapi/v1/img2img' : '/sdapi/v1/txt2img'
case 'naifu':
return '/generate-stream'
default:
return '/ai/generate-image'
}
})()

const request = () => ctx.http.axios(trimSlash(config.endpoint) + path, {
method: 'POST',
timeout: config.requestTimeout,
headers: {
...config.headers,
authorization: 'Bearer ' + token,
},
data: getPostData(),
data,
}).then((res) => {
if (config.type === 'sd-webui') {
return (res.data as StableDiffusionWebUI.Response).images[0]
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ commands:
- 你就在此地不要走动,等我给你画一幅
pending: 在画了在画了,不过前面还有 {0} 个稿……
invalid-size: 增强功能仅适用于被生成的图片。普通的 img2img 请直接使用「约稿」而不是「增强」。
invalid-resolution: 非法的图片尺寸。宽高必须都为 64 的倍数且宽高和不能超过 1280。
file-too-large: 文件体积过大。
unsupported-file-type: 不支持的文件格式。
download-error: 图片解析失败。
Expand Down

0 comments on commit 824d42b

Please sign in to comment.