|
| 1 | +/* |
| 2 | + * This file is part of the Wildfire Chat package. |
| 3 | + * (c) Heavyrain2012 <heavyrain.lee@gmail.com> |
| 4 | + * |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + */ |
| 8 | + |
| 9 | +package io.moquette.imhandler; |
| 10 | + |
| 11 | +import cn.wildfirechat.common.ErrorCode; |
| 12 | +import cn.wildfirechat.proto.WFCMessage; |
| 13 | +import com.qiniu.util.Auth; |
| 14 | +import com.xiaoleilu.loServer.action.UploadFileAction; |
| 15 | +import io.moquette.server.config.MediaServerConfig; |
| 16 | +import io.moquette.spi.impl.Qos1PublishHandler; |
| 17 | +import io.netty.buffer.ByteBuf; |
| 18 | + |
| 19 | +@Handler("GMUT") |
| 20 | +public class GetMediaUploadTokenHandler extends IMHandler<WFCMessage.GetUploadTokenRequest> { |
| 21 | + @Override |
| 22 | + public ErrorCode action(ByteBuf ackPayload, String clientID, String fromUser, boolean isAdmin, WFCMessage.GetUploadTokenRequest request, Qos1PublishHandler.IMCallback callback) { |
| 23 | + int type = request.getMediaType(); |
| 24 | + |
| 25 | + String token; |
| 26 | + |
| 27 | + WFCMessage.GetUploadTokenResult.Builder resultBuilder = WFCMessage.GetUploadTokenResult.newBuilder(); |
| 28 | + if (MediaServerConfig.USER_QINIU) { |
| 29 | + Auth auth = Auth.create(MediaServerConfig.QINIU_ACCESS_KEY, MediaServerConfig.QINIU_SECRET_KEY); |
| 30 | + |
| 31 | + |
| 32 | + String bucketName; |
| 33 | + String bucketDomain; |
| 34 | + switch (type) { |
| 35 | + case 0: |
| 36 | + bucketName = MediaServerConfig.QINIU_BUCKET_GENERAL_NAME; |
| 37 | + bucketDomain = MediaServerConfig.QINIU_BUCKET_GENERAL_DOMAIN; |
| 38 | + break; |
| 39 | + case 1: |
| 40 | + bucketName = MediaServerConfig.QINIU_BUCKET_IMAGE_NAME; |
| 41 | + bucketDomain = MediaServerConfig.QINIU_BUCKET_IMAGE_DOMAIN; |
| 42 | + break; |
| 43 | + case 2: |
| 44 | + bucketName = MediaServerConfig.QINIU_BUCKET_VOICE_NAME; |
| 45 | + bucketDomain = MediaServerConfig.QINIU_BUCKET_VOICE_DOMAIN; |
| 46 | + break; |
| 47 | + case 3: |
| 48 | + bucketName = MediaServerConfig.QINIU_BUCKET_VIDEO_NAME; |
| 49 | + bucketDomain = MediaServerConfig.QINIU_BUCKET_VIDEO_DOMAIN; |
| 50 | + break; |
| 51 | + case 4: |
| 52 | + bucketName = MediaServerConfig.QINIU_BUCKET_FILE_NAME; |
| 53 | + bucketDomain = MediaServerConfig.QINIU_BUCKET_FILE_DOMAIN; |
| 54 | + break; |
| 55 | + case 5: |
| 56 | + bucketName = MediaServerConfig.QINIU_BUCKET_PORTRAIT_NAME; |
| 57 | + bucketDomain = MediaServerConfig.QINIU_BUCKET_PORTRAIT_DOMAIN; |
| 58 | + break; |
| 59 | + case 6: |
| 60 | + bucketName = MediaServerConfig.QINIU_BUCKET_FAVORITE_NAME; |
| 61 | + bucketDomain = MediaServerConfig.QINIU_BUCKET_FAVORITE_DOMAIN; |
| 62 | + break; |
| 63 | + default: |
| 64 | + bucketName = MediaServerConfig.QINIU_BUCKET_GENERAL_NAME; |
| 65 | + bucketDomain = MediaServerConfig.QINIU_BUCKET_GENERAL_DOMAIN; |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + token = auth.uploadToken(bucketName); |
| 70 | + resultBuilder.setDomain(bucketDomain) |
| 71 | + .setServer(MediaServerConfig.QINIU_SERVER_URL); |
| 72 | + resultBuilder.setPort(80); |
| 73 | + } else { |
| 74 | + token = UploadFileAction.getToken(type); |
| 75 | + resultBuilder.setDomain("http://" + MediaServerConfig.SERVER_IP + ":" + MediaServerConfig.HTTP_SERVER_PORT) |
| 76 | + .setServer(MediaServerConfig.SERVER_IP); |
| 77 | + resultBuilder.setPort(MediaServerConfig.HTTP_SERVER_PORT); |
| 78 | + } |
| 79 | + |
| 80 | + resultBuilder.setToken(token); |
| 81 | + |
| 82 | + byte[] data = resultBuilder.buildPartial().toByteArray(); |
| 83 | + ackPayload.ensureWritable(data.length).writeBytes(data); |
| 84 | + return ErrorCode.ERROR_CODE_SUCCESS; |
| 85 | + } |
| 86 | +} |
0 commit comments