chat_ui/mixins/fileMixin.js
2025-03-16 12:45:43 +03:30

147 lines
4.5 KiB
JavaScript

import chatApi from "~/apis/chatApi";
import repoApi from "~/apis/repoApi";
export default {
methods: {
removeFile(file, index) {
this.files.splice(index, 1);
this.filenames.splice(index, 1);
if (this.files.length == 0) this.showDroppedList = false;
},
async mainSaveFiles() {
if (this.uploading) return;
this.uploading = true;
if (this.saveAs == "replay") {
this.replaySaveFiles();
} else {
let payload = {
text: this.uploadDescription,
group_id: this.listGetter.id,
id: this.mainCommentor?.id ?? undefined,
contact_id: undefined,
replyto: undefined,
};
if (this.$route.name == "privates") {
payload = {
text: this.uploadDescription,
id: this.mainCommentor?.id ?? undefined,
contact_id: this.listGetter.user,
};
}
let url = chatApi.messages.create;
if (this.files.length > 1) {
this.files.forEach((fileItem) => {
payload.file = fileItem;
payload.length = fileItem.size;
this.httpService
.formDataRequest(messageUrl() +url, payload)
.then((res) => {
// this.closeDroppedList();
// this.toLastItem();
})
.catch((err) => {
this.mySwalToast({
html: "حداکثر حجم فایل برای ارسال 50 مگابایت می باشد.",
icon: "error",
});
})
.finally(() => {
this.uploading = false;
});
});
this.userMessage = null;
this.uploadDescription = null;
this.closeDroppedList();
this.toLastItem();
} else {
// 0: files, 1: image, 2:audio 3:video
// payload.file_type = this.getFileType(this.files[0].name);
payload.file = this.files[0];
payload.length = this.files[0].size;
return await this.httpService
.formDataRequest(messageUrl() +url, payload)
.then((res) => {
this.closeDroppedList();
// when clicking on the attach file btn.
this.userMessage = null;
this.uploadDescription = null;
this.closeDroppedList();
this.toLastItem();
})
.catch((err) => {
this.mySwalToast({
html: "حداکثر حجم فایل برای ارسال 50 مگابایت می باشد.",
icon: "error",
});
})
.finally(() => {
this.uploading = false;
});
}
}
},
endOfUploading() {
},
startUploading() {
},
openUploadForm(dropBoxClass) {
this.files = [];
this.uploadDescription = null;
this.showDroppedList = true;
this.dropBoxClass = dropBoxClass;
this.showMobileUploadButton = true;
},
setImage(file) {
this.hasImage = true;
this.files.push(file);
},
getFileType(fileName) {
if (
["jpg", "jpeg", "png", "webp"].includes(this.getFileExtension(fileName))
)
return 1;
if (
["mp3", "wav", "png", "webp"].includes(this.getFileExtension(fileName))
)
return 2;
if (["mp4", "mpeg", "mkv"].includes(this.getFileExtension(fileName)))
return 3;
return 0;
},
fileIcon(type) {
if (type == "text/plain") return "txt-file";
else if (type == "pdf-file") return "pdf-file";
else if (type == "doc-file") return "doc-file";
else if (type == "docx-file") return "docx-file";
else if (type == "excel-file") return "excel-file";
else if (type == "csv-file") return "csv-file";
else if (type == "word-file") return "word-file";
else if (type == "ppt-file") return "ppt-file";
else if (type == "mp3-file") return "mp3-file";
else if (type == "jpg-file") return "jpg-file";
else if (type == "png-file") return "png-file";
else if (type == "mov-file") return "mov-file";
else if (type == "rar-file") return "rar-file";
else if (type == "zip-file") return "zip-file";
else return "other-file";
},
closeDroppedList() {
this.files = [];
this.showDroppedList = false;
this.pasting = false;
this.showMobileUploadButton = false;
this.uploadDescription = null;
},
},
};