QString FormattedMessage::saveInImagesPath(const QString &filePath) { QFileInfo fileInfo(filePath); if (!fileInfo.exists()) return filePath; QFileInfo imagesPathInfo(ChatImageService::imagesPath()); if (!imagesPathInfo.isDir() && !QDir().mkdir(imagesPathInfo.absolutePath())) return filePath; // if already in imagesPath, it'd be a waste to copy it in the same dir if (fileInfo.absolutePath() == imagesPathInfo.absolutePath()) return fileInfo.fileName(); QString copyFileName = QUuid::createUuid().toString(); // Make this file name less exotic. First, get rid of '{' and '}' on edges. if (copyFileName.length() > 2) copyFileName = copyFileName.mid(1, copyFileName.length() - 2); // Second, try to add extension. QString ext = fileInfo.completeSuffix(); if (!ext.isEmpty()) copyFileName += '.' + ext; if (QFile::copy(filePath, imagesPathInfo.absolutePath() + '/' + copyFileName)) return copyFileName; return filePath; }
QString ImageStorageServiceImpl::storeImage(const QString &imageFilePath) { QFileInfo fileInfo(imageFilePath); if (!fileInfo.exists()) return imageFilePath; QFileInfo imagesPathInfo(storagePath()); if (fileInfo.absolutePath() == imagesPathInfo.absolutePath()) return fileInfo.fileName(); if (!imagesPathInfo.isDir() && !QDir().mkdir(imagesPathInfo.absolutePath())) return imageFilePath; QString copyFileName = QUuid::createUuid().toString(); // Make this file name less exotic. First, get rid of '{' and '}' on edges. if (copyFileName.length() > 2) copyFileName = copyFileName.mid(1, copyFileName.length() - 2); // Second, try to add extension. QString ext = fileInfo.completeSuffix(); if (!ext.isEmpty()) copyFileName += '.' + ext; if (QFile::copy(imageFilePath, imagesPathInfo.absolutePath() + '/' + copyFileName)) return copyFileName; return imageFilePath; }