Example #1
0
        /// <summary>
        /// Encrypt a file message and send it to the given recipient.
        /// The thumbnailMessagePath can be null.
        /// </summary>
        /// <param name="threemaId">target Threema ID</param>
        /// <param name="fileMessageFile">the file to be sent</param>
        /// <param name="thumbnailMessageFile">file for thumbnail; if not set, no thumbnail will be sent</param>
        /// <returns>generated message ID</returns>
        public string SendFileMessage(string threemaId, FileInfo fileMessageFile, FileInfo thumbnailMessageFile)
        {
            //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();
            }

            if (fileMessageFile == null)
            {
                throw new ArgumentException("fileMessageFile must not be null.");
            }

            if (!fileMessageFile.Exists)
            {
                throw new FileNotFoundException(fileMessageFile.FullName);
            }

            byte[] fileData;
            using (Stream stream = File.OpenRead(fileMessageFile.FullName))
            {
                fileData = new byte[stream.Length];
                stream.Read(fileData, 0, (int)stream.Length);
                stream.Close();
            }

            if (fileData == null)
            {
                throw new IOException("invalid file");
            }

            //encrypt the image
            EncryptResult encryptResult = CryptTool.EncryptFileData(fileData);

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

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

            UploadResult uploadResultThumbnail = null;

            if (thumbnailMessageFile != null && thumbnailMessageFile.Exists)
            {
                byte[] thumbnailData;
                using (Stream stream = File.OpenRead(thumbnailMessageFile.FullName))
                {
                    thumbnailData = new byte[stream.Length];
                    stream.Read(thumbnailData, 0, (int)stream.Length);
                    stream.Close();
                }

                if (thumbnailData == null)
                {
                    throw new IOException("invalid thumbnail file");
                }

                //encrypt the thumbnail
                EncryptResult encryptResultThumbnail = CryptTool.encryptFileThumbnailData(fileData, encryptResult.Secret);

                //upload the thumbnail
                uploadResultThumbnail = this.apiConnector.UploadFile(encryptResultThumbnail);
            }

            //send it
            EncryptResult fileMessage = CryptTool.EncryptFileMessage(
                encryptResult,
                uploadResult,
                GetMIMEType(fileMessageFile),
                fileMessageFile.Name,
                (int)fileMessageFile.Length,
                uploadResultThumbnail,
                privateKey, publicKey);

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