| 8857 | 'charx', | 8857 | 'charx', |
| 8858 | ]; | 8858 | ]; |
| 8859 | | 8859 | |
| | 8860 | const avatarFileNames = []; |
| 8860 | for (const file of files) { | 8861 | for (const file of files) { |
| 8861 | const extension = file.name.split('.').pop().toLowerCase(); | 8862 | const extension = file.name.split('.').pop().toLowerCase(); |
| 8862 | if (allowedMimeTypes.some(x => file.type.startsWith(x)) || allowedExtensions.includes(extension)) { | 8863 | if (allowedMimeTypes.some(x => file.type.startsWith(x)) || allowedExtensions.includes(extension)) { |
| 8863 | const preservedName = data instanceof Map && data.get(file); | 8864 | const preservedName = data instanceof Map && data.get(file); |
| 8864 | await importCharacter(file, preservedName); | 8865 | const avatarFileName = await importCharacter(file, { preserveFileName: preservedName }); |
| | 8866 | if (avatarFileName !== undefined) { |
| | 8867 | avatarFileNames.push(avatarFileName); |
| | 8868 | } |
| 8865 | } else { | 8869 | } else { |
| 8866 | toastr.warning(t`Unsupported file type: ` + file.name); | 8870 | toastr.warning(t`Unsupported file type: ` + file.name); |
| 8867 | } | 8871 | } |
| 8868 | } | 8872 | } |
| | 8873 | |
| | 8874 | if (avatarFileNames.length > 0) { |
| | 8875 | await importCharactersTags(avatarFileNames); |
| | 8876 | selectImportedChar(avatarFileNames[avatarFileNames.length - 1]); |
| | 8877 | } |
| | 8878 | } |
| | 8879 | |
| | 8880 | /** |
| | 8881 | * Imports tags for the given characters |
| | 8882 | * @param {string[]} avatarFileNames character avatar filenames whose tags are to import |
| | 8883 | */ |
| | 8884 | async function importCharactersTags(avatarFileNames) { |
| | 8885 | await getCharacters(); |
| | 8886 | for (let i = 0; i < avatarFileNames.length; i++) { |
| | 8887 | if (power_user.tag_import_setting !== tag_import_setting.NONE) { |
| | 8888 | const importedCharacter = characters.find(character => character.avatar === avatarFileNames[i]); |
| | 8889 | await importTags(importedCharacter); |
| | 8890 | } |
| | 8891 | } |
| | 8892 | } |
| | 8893 | |
| | 8894 | /** |
| | 8895 | * Selects the given imported char |
| | 8896 | * @param {string} charId char to select |
| | 8897 | */ |
| | 8898 | function selectImportedChar(charId) { |
| | 8899 | let oldSelectedChar = null; |
| | 8900 | if (this_chid !== undefined) { |
| | 8901 | oldSelectedChar = characters[this_chid].avatar; |
| | 8902 | } |
| | 8903 | select_rm_info('char_import_no_toast', charId, oldSelectedChar); |
| 8869 | } | 8904 | } |
| 8870 | | 8905 | |
| 8871 | /** | 8906 | /** |
| 8872 | * Imports a character from a file. | 8907 | * Imports a character from a file. |
| 8873 | * @param {File} file File to import | 8908 | * @param {File} file File to import |
| 8874 | * @param {string?} preserveFileName Whether to preserve original file name | 8909 | * @param {object} [options] - Options |
| 8875 | * @returns {Promise<void>} | 8910 | * @param {string} [options.preserveFileName] Whether to preserve original file name |
| | 8911 | * @param {Boolean} [options.importTags=false] Whether to import tags |
| | 8912 | * @returns {Promise<string>} |
| 8876 | */ | 8913 | */ |
| 8877 | async function importCharacter(file, preserveFileName = '') { | 8914 | async function importCharacter(file, { preserveFileName = '', importTags = false } = {}) { |
| 8878 | if (is_group_generating || is_send_press) { | 8915 | if (is_group_generating || is_send_press) { |
| 8879 | toastr.error(t`Cannot import characters while generating. Stop the request and try again.`, t`Import aborted`); | 8916 | toastr.error(t`Cannot import characters while generating. Stop the request and try again.`, t`Import aborted`); |
| 8880 | throw new Error('Cannot import character while generating'); | 8917 | throw new Error('Cannot import character while generating'); |