Skip to content

Commit

Permalink
v5.12.2
Browse files Browse the repository at this point in the history
  • Loading branch information
trueai-org committed Nov 11, 2024
1 parent eed509c commit cd02e75
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 61 deletions.
28 changes: 18 additions & 10 deletions src/Midjourney.API/Controllers/SubmitController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,20 @@ public async Task<ActionResult<SubmitResultVO>> ImagineDiscordImages([FromBody]
return Ok(Message.Of(uploadResult.Code, uploadResult.Description));
}

var finalFileName = uploadResult.Description;
var sendImageResult = await instance.SendImageMessageAsync("upload image: " + finalFileName, finalFileName);
if (sendImageResult.Code != ReturnCode.SUCCESS)
if (uploadResult.Description.StartsWith("http"))
{
return Ok(Message.Of(sendImageResult.Code, sendImageResult.Description));
imageUrls.Add(uploadResult.Description);
}
else
{
var finalFileName = uploadResult.Description;
var sendImageResult = await instance.SendImageMessageAsync("upload image: " + finalFileName, finalFileName);
if (sendImageResult.Code != ReturnCode.SUCCESS)
{
return Ok(Message.Of(sendImageResult.Code, sendImageResult.Description));
}
imageUrls.Add(sendImageResult.Description);
}
imageUrls.Add(sendImageResult.Description);
}

var info = SubmitResultVO.Of(ReturnCode.SUCCESS, "提交成功", imageUrls);
Expand Down Expand Up @@ -471,11 +478,12 @@ public ActionResult<SubmitResultVO> Blend([FromBody] SubmitBlendDTO blendDTO)
return Ok(SubmitResultVO.Fail(ReturnCode.VALIDATION_ERROR, "base64List参数错误"));
}

var setting = GlobalConfiguration.Setting;
if (!setting.EnableUserCustomUploadBase64)
{
return Ok(SubmitResultVO.Fail(ReturnCode.VALIDATION_ERROR, "禁止上传"));
}
// blend 不限制上传
//var setting = GlobalConfiguration.Setting;
//if (!setting.EnableUserCustomUploadBase64)
//{
// return Ok(SubmitResultVO.Fail(ReturnCode.VALIDATION_ERROR, "禁止上传"));
//}

if (blendDTO.Dimensions == null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Midjourney.API/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<title>Midjourney Proxy Admin</title>
<link rel="stylesheet" href="/umi.db3f196a.css">
<script async src="/scripts/loading.js"></script>
<script src="/preload_helper.1f0e6889.js"></script>
<script src="/preload_helper.3fd81c95.js"></script>
</head>
<body>
<div id="root"></div>
<script src="/umi.b26084e0.js"></script>
<script src="/umi.621049bb.js"></script>
</body>
</html>

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Midjourney.Infrastructure/GlobalConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class GlobalConfiguration
/// <summary>
/// 版本号
/// </summary>
public static string Version { get; set; } = "v5.12.1";
public static string Version { get; set; } = "v5.12.2";

/// <summary>
/// 全局配置项
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@
<Content Remove="Resources\ApiParams\fastniji.json" />
<Content Remove="Resources\ApiParams\relax.json" />
<Content Remove="Resources\ApiParams\relaxniji.json" />
<Content Remove="Resources\ApiParams\describelink.json" />
<Content Remove="Resources\ApiParams\describenijilink.json" />
</ItemGroup>


<ItemGroup>
<EmbeddedResource Include="Resources\ApiParams\describelink.json" />
<EmbeddedResource Include="Resources\ApiParams\describenijilink.json" />
<EmbeddedResource Include="Resources\ApiParams\fast.json" />
<EmbeddedResource Include="Resources\ApiParams\fastniji.json" />
<EmbeddedResource Include="Resources\ApiParams\relaxniji.json" />
Expand Down
5 changes: 5 additions & 0 deletions src/Midjourney.Infrastructure/Models/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public class Setting : ProxyProperties
/// 启用转换官方链接
/// </summary>
public bool EnableConvertOfficialLink { get; set; } = true;

/// <summary>
/// 启用转换阿里云链接
/// </summary>
public bool EnableConvertAliyunLink { get; set; }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"type": 2,
"guild_id": "$guild_id",
"channel_id": "$channel_id",
"application_id": "936929561302675456",
"session_id": "$session_id",
"nonce": "$nonce",
"data": {
"version": "1237876415471554625",
"id": "1092492867185950852",
"name": "describe",
"type": 1,
"options": [
{
"type": 3,
"name": "link",
"value": "$link"
}
],
"attachments": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"type": 2,
"guild_id": "$guild_id",
"channel_id": "$channel_id",
"application_id": "1022952195194359889",
"session_id": "$session_id",
"nonce": "$nonce",
"data": {
"version": "1248805223892254776",
"id": "1092683500743831652",
"name": "describe",
"type": 1,
"options": [
{
"type": 3,
"name": "link",
"value": "$link"
}
],
"attachments": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public AliyunOssStorageService()
_endpoint = ossOptions.Endpoint!;
}


public AliyunOssOptions Options => _ossOptions;

/// <summary>
/// 水印 base64 编码
/// </summary>
Expand Down
133 changes: 98 additions & 35 deletions src/Midjourney.Infrastructure/Services/DiscordInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
using Midjourney.Infrastructure.Services;
using Midjourney.Infrastructure.Util;
using Newtonsoft.Json.Linq;
using RestSharp;
using Serilog;
using System.Collections.Concurrent;
using System.Diagnostics;
Expand Down Expand Up @@ -1385,20 +1384,26 @@ public async Task<string> FormatUrls(string prompt)
{
// 上传到 Discord 服务器
var uploadResult = await UploadAsync(res.FileName, new DataUrl(res.ContentType, res.FileBytes));
if (uploadResult.Code != ReturnCode.SUCCESS)
{
throw new LogicException(uploadResult.Code, uploadResult.Description);
}
var finalFileName = uploadResult.Description;
var sendImageResult = await SendImageMessageAsync("upload image: " + finalFileName, finalFileName);
if (sendImageResult.Code != ReturnCode.SUCCESS)
if (uploadResult.Description.StartsWith("http"))
{
throw new LogicException(sendImageResult.Code, sendImageResult.Description);
return uploadResult.Description;
}
else
{
var finalFileName = uploadResult.Description;
var sendImageResult = await SendImageMessageAsync("upload image: " + finalFileName, finalFileName);
if (sendImageResult.Code != ReturnCode.SUCCESS)
{
throw new LogicException(sendImageResult.Code, sendImageResult.Description);
}
return sendImageResult.Description;
return sendImageResult.Description;
}
}
throw new LogicException($"解析链接失败 {url}, {res?.Msg}");
Expand Down Expand Up @@ -1518,6 +1523,21 @@ public async Task<Message> DescribeAsync(string finalFileName, string nonce, EBo
return await PostJsonAndCheckStatusAsync(paramsStr);
}

/// <summary>
/// 解析描述
/// </summary>
/// <param name="link"></param>
/// <param name="nonce"></param>
/// <param name="botType"></param>
/// <returns></returns>
public async Task<Message> DescribeByLinkAsync(string link, string nonce, EBotType botType)
{
var json = botType == EBotType.NIJI_JOURNEY ? _paramsMap["describenijilink"] : _paramsMap["describelink"];
string paramsStr = ReplaceInteractionParams(json, nonce)
.Replace("$link", link);
return await PostJsonAndCheckStatusAsync(paramsStr);
}

/// <summary>
/// 上传一个较长的提示词,mj 可以返回一组简要的提示词
/// </summary>
Expand Down Expand Up @@ -1609,43 +1629,86 @@ private string ReplaceInteractionParams(string paramsStr, string nonce, EBotType
return str;
}

public async Task<Message> UploadAsync(string fileName, DataUrl dataUrl)
public async Task<Message> UploadAsync(string fileName, DataUrl dataUrl, bool useDiscordUpload = false)
{
try
// 启用转为阿里链接
if (GlobalConfiguration.Setting.EnableConvertAliyunLink && !useDiscordUpload)
{
JObject fileObj = new JObject
{
["filename"] = fileName,
["file_size"] = dataUrl.Data.Length,
["id"] = "0"
};
JObject paramsJson = new JObject
{
["files"] = new JArray { fileObj }
};
HttpResponseMessage response = await PostJsonAsync(_discordAttachmentUrl, paramsJson.ToString());
if (response.StatusCode != System.Net.HttpStatusCode.OK)
try
{
_logger.Error("上传图片到discord失败, status: {StatusCode}, msg: {Body}", response.StatusCode, await response.Content.ReadAsStringAsync());
return Message.Of(ReturnCode.VALIDATION_ERROR, "上传图片到discord失败");
var oss = new AliyunOssStorageService();

var localPath = $"attachments/{DateTime.Now:yyyyMMdd}/{Guid.NewGuid():N}/{fileName}";

var mt = MimeKit.MimeTypes.GetMimeType(Path.GetFileName(localPath));
if (string.IsNullOrWhiteSpace(mt))
{
mt = "image/png";
}

var stream = new MemoryStream(dataUrl.Data);
var res = oss.SaveAsync(stream, localPath, dataUrl.MimeType ?? mt);
if (string.IsNullOrWhiteSpace(res?.Key))
{
throw new Exception("上传图片到阿里云失败");
}

// 替换 url
var customCdn = oss.Options.CustomCdn;
if (string.IsNullOrWhiteSpace(customCdn))
{
customCdn = oss.Options.Endpoint;
}

var url = $"{customCdn?.Trim()?.Trim('/')}/{res.Key}";

return Message.Success(url);
}
JArray array = JObject.Parse(await response.Content.ReadAsStringAsync())["attachments"] as JArray;
if (array == null || array.Count == 0)
catch (Exception e)
{
return Message.Of(ReturnCode.VALIDATION_ERROR, "上传图片到discord失败");
}
string uploadUrl = array[0]["upload_url"].ToString();
string uploadFilename = array[0]["upload_filename"].ToString();

await PutFileAsync(uploadUrl, dataUrl);
_logger.Error(e, "上传图片到阿里云失败");

return Message.Success(uploadFilename);
return Message.Of(ReturnCode.FAILURE, "上传图片到阿里云失败");
}
}
catch (Exception e)
else
{
_logger.Error(e, "上传图片到discord失败");
try
{
JObject fileObj = new JObject
{
["filename"] = fileName,
["file_size"] = dataUrl.Data.Length,
["id"] = "0"
};
JObject paramsJson = new JObject
{
["files"] = new JArray { fileObj }
};
HttpResponseMessage response = await PostJsonAsync(_discordAttachmentUrl, paramsJson.ToString());
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
_logger.Error("上传图片到discord失败, status: {StatusCode}, msg: {Body}", response.StatusCode, await response.Content.ReadAsStringAsync());
return Message.Of(ReturnCode.VALIDATION_ERROR, "上传图片到discord失败");
}
JArray array = JObject.Parse(await response.Content.ReadAsStringAsync())["attachments"] as JArray;
if (array == null || array.Count == 0)
{
return Message.Of(ReturnCode.VALIDATION_ERROR, "上传图片到discord失败");
}
string uploadUrl = array[0]["upload_url"].ToString();
string uploadFilename = array[0]["upload_filename"].ToString();

await PutFileAsync(uploadUrl, dataUrl);

return Message.Success(uploadFilename);
}
catch (Exception e)
{
_logger.Error(e, "上传图片到discord失败");

return Message.Of(ReturnCode.FAILURE, "上传图片到discord失败");
return Message.Of(ReturnCode.FAILURE, "上传图片到discord失败");
}
}
}

Expand Down
Loading

0 comments on commit cd02e75

Please sign in to comment.