コード例 #1
0
        /// <summary>
        /// Encrypt an image message and send it to the given recipient.
        /// </summary>
        /// <param name="threemaId">threemaId target Threema ID</param>
        /// <param name="imageFilePath">path to read image data from</param>
        /// <returns>generated message ID</returns>
        public string SendImageMessage(string threemaId, string imageFilePath)
        {
            //fetch public key
            byte[] publicKey = this.apiConnector.LookupKey(threemaId);

            if (publicKey == null)
            {
                throw new InvalidKeyException("invalid threema id");
            }

            //check capability of a key
            CapabilityResult capabilityResult = this.apiConnector.LookupKeyCapability(threemaId);

            if (capabilityResult == null || !capabilityResult.CanImage)
            {
                throw new NotAllowedException();
            }

            byte[] fileData = File.ReadAllBytes(imageFilePath);
            if (fileData == null)
            {
                throw new IOException("invalid file");
            }

            //encrypt the image
            EncryptResult encryptResult = CryptTool.Encrypt(fileData, this.privateKey, publicKey);

            //upload the image
            UploadResult uploadResult = apiConnector.UploadFile(encryptResult);

            if (!uploadResult.IsSuccess)
            {
                throw new IOException("could not upload file (upload response " + uploadResult.ResponseCode + ")");
            }

            //send it
            EncryptResult imageMessage = CryptTool.EncryptImageMessage(encryptResult, uploadResult, this.privateKey, publicKey);

            return(apiConnector.SendE2EMessage(
                       threemaId,
                       imageMessage.Nonce,
                       imageMessage.Result));
        }