Search in sources :

Example 1 with BucketManager

use of com.qiniu.storage.BucketManager in project symphony by b3log.

the class AudioMgmtService method tts.

/**
 * Text to speech.
 *
 * @param text   the specified text
 * @param type   specified type, "article"/"comment"
 * @param textId the specified id of text, article id or comment id
 * @param uid    the specified user id
 * @return speech URL, returns {@code ""} if TTS failed
 */
public String tts(final String text, final String type, final String textId, final String uid) {
    final byte[] bytes = baiduTTS(text, uid);
    if (null == bytes) {
        return "";
    }
    String ret;
    try {
        if (Symphonys.getBoolean("qiniu.enabled")) {
            final Auth auth = Auth.create(Symphonys.get("qiniu.accessKey"), Symphonys.get("qiniu.secretKey"));
            final UploadManager uploadManager = new UploadManager(new Configuration());
            final BucketManager bucketManager = new BucketManager(auth, new Configuration());
            final int seq = RandomUtils.nextInt(10);
            final String fileKey = "audio/" + type + "/" + textId + seq + ".mp3";
            try {
                bucketManager.delete(Symphonys.get("qiniu.bucket"), fileKey);
            } catch (final Exception e) {
            // ignore
            }
            uploadManager.put(bytes, fileKey, auth.uploadToken(Symphonys.get("qiniu.bucket")), null, "audio/mp3", false);
            ret = Symphonys.get("qiniu.domain") + "/audio/" + type + "/" + textId + seq + ".mp3";
        } else {
            final String fileName = UUID.randomUUID().toString().replaceAll("-", "") + ".mp3";
            final OutputStream output = new FileOutputStream(Symphonys.get("upload.dir") + fileName);
            IOUtils.write(bytes, output);
            IOUtils.closeQuietly(output);
            ret = Latkes.getServePath() + "/upload/" + fileName;
        }
        return ret;
    } catch (final Exception e) {
        if (e instanceof QiniuException) {
            try {
                LOGGER.log(Level.ERROR, "Uploads audio failed [type=" + type + ", textId=" + textId + "], Qiniu exception body [" + ((QiniuException) e).response.bodyString() + "]");
            } catch (final Exception qe) {
                LOGGER.log(Level.ERROR, "Uploads audio and parse result exception", qe);
            }
        } else {
            LOGGER.log(Level.ERROR, "Uploads audio failed [type=" + type + ", textId=" + textId + "]", e);
        }
    }
    return "";
}
Also used : QiniuException(com.qiniu.common.QiniuException) Configuration(com.qiniu.storage.Configuration) BucketManager(com.qiniu.storage.BucketManager) Auth(com.qiniu.util.Auth) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) UploadManager(com.qiniu.storage.UploadManager) QiniuException(com.qiniu.common.QiniuException)

Example 2 with BucketManager

use of com.qiniu.storage.BucketManager in project paascloud-master by paascloud.

the class QiniuOssConfiguration method bucketManager.

/**
 * Bucket manager bucket manager.
 *
 * @return the bucket manager
 */
@Bean
public BucketManager bucketManager() {
    Zone zone = Zone.autoZone();
    // 创建上传对象
    BucketManager uploadManager = new BucketManager(auth(), new com.qiniu.storage.Configuration(zone));
    log.info("Create BucketManager OK.");
    return uploadManager;
}
Also used : Zone(com.qiniu.common.Zone) BucketManager(com.qiniu.storage.BucketManager) Bean(org.springframework.context.annotation.Bean)

Aggregations

BucketManager (com.qiniu.storage.BucketManager)2 QiniuException (com.qiniu.common.QiniuException)1 Zone (com.qiniu.common.Zone)1 Configuration (com.qiniu.storage.Configuration)1 UploadManager (com.qiniu.storage.UploadManager)1 Auth (com.qiniu.util.Auth)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 Bean (org.springframework.context.annotation.Bean)1