Skip to content

Commit

Permalink
feat: improve upload path format for image storage (casibase#758)
Browse files Browse the repository at this point in the history
* feat: improve image display in input box

* feat: improve upload path format for image storage
  • Loading branch information
MartinRepo authored Mar 7, 2024
1 parent b1dd078 commit 438dc20
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions controllers/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (c *ApiController) AddMessage() {
ReplyTo: message.GetId(),
Author: "AI",
Text: "",
FileName: message.FileName,
VectorScores: []object.VectorScore{},
}
_, err = object.AddMessage(answerMessage)
Expand Down
9 changes: 5 additions & 4 deletions object/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ type Message struct {
Name string `xorm:"varchar(100) notnull pk" json:"name"`
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`

// Organization string `xorm:"varchar(100)" json:"organization"`
Organization string `xorm:"varchar(100)" json:"organization"`
User string `xorm:"varchar(100) index" json:"user"`
Chat string `xorm:"varchar(100) index" json:"chat"`
ReplyTo string `xorm:"varchar(100) index" json:"replyTo"`
Author string `xorm:"varchar(100)" json:"author"`
Text string `xorm:"mediumtext" json:"text"`
FileName string `xorm:"varchar(100)" json:"fileName"`
Comment string `xorm:"mediumtext" json:"comment"`
TokenCount int `json:"tokenCount"`
Price float64 `json:"price"`
Expand Down Expand Up @@ -163,13 +164,13 @@ func RefineMessageImages(message *Message, origin string) error {
return err
}

for i, match := range matches {
ext, content, err := parseBase64Image(match)
for _, match := range matches {
_, content, err := parseBase64Image(match)
if err != nil {
return err
}

filename := fmt.Sprintf("%s_%s_image_%d.%s", message.User, message.Name, i, ext)
filename := fmt.Sprintf(`%s\%s\%s\%s`, message.Organization, message.User, message.Chat, message.FileName)

fileUrl, err := obj.PutObject(message.User, "Casibase-Image", filename, bytes.NewBuffer(content))
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion web/src/ChatBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ class ChatBox extends React.Component {
const newValue = this.state.value.replace(/<img src="([^"]*)" alt="([^"]*)" width="(\d+)" height="(\d+)" data-original-width="(\d+)" data-original-height="(\d+)">/g, (match, src, alt, width, height, scaledWidth, scaledHeight) => {
return `<img src="${src}" alt="${alt}" width="${scaledWidth}" height="${scaledHeight}">`;
});
this.props.sendMessage(newValue);
let fileName = "";
if (this.inputImage.files[0]) {
fileName = this.inputImage.files[0].name;
}
this.props.sendMessage(newValue, fileName);
this.setState({value: ""});
};

Expand Down
11 changes: 6 additions & 5 deletions web/src/ChatPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,25 @@ class ChatPage extends BaseListPage {
};
}

newMessage(text, isHidden) {
newMessage(text, fileName, isHidden) {
const randomName = Setting.getRandomName();
return {
owner: "admin",
name: `message_${randomName}`,
createdTime: moment().format(),
// organization: this.props.account.owner,
organization: this.props.account.owner,
user: this.props.account.name,
chat: this.state.chat?.name,
replyTo: "",
author: `${this.props.account.owner}/${this.props.account.name}`,
text: text,
isHidden: isHidden,
fileName: fileName,
};
}

sendMessage(text, isHidden) {
const newMessage = this.newMessage(text, isHidden);
sendMessage(text, fileName, isHidden) {
const newMessage = this.newMessage(text, fileName, isHidden);
MessageBackend.addMessage(newMessage)
.then((res) => {
if (res.status === "ok") {
Expand Down Expand Up @@ -372,7 +373,7 @@ class ChatPage extends BaseListPage {
</div>
)
}
<ChatBox disableInput={this.state.disableInput} messages={this.state.messages} sendMessage={(text) => {this.sendMessage(text, false);}} account={this.props.account} />
<ChatBox disableInput={this.state.disableInput} messages={this.state.messages} sendMessage={(text, fileName) => {this.sendMessage(text, fileName, false);}} account={this.props.account} />
</div>
</div>
);
Expand Down

0 comments on commit 438dc20

Please sign in to comment.