public CapabilityResult GetPage(string filter, int?draw, int?initialPage, int?pageSize, string sortDir, string sortBy) { using (SATEntities db = new SATEntities()) { var data = db.vw_Capability.ToList(); int recordsTotal = data.Count(); if (!string.IsNullOrEmpty(filter)) { data = data.Where(x => x.CapTName.Contains(filter) || x.CapGName.Contains(filter) || x.CapGTName.Contains(filter)).ToList(); } int recordsFiltered = data.Count(); switch (sortBy) { case "CapYear": data = (sortDir == "asc") ? data.OrderBy(x => x.CapYear).ToList() : data.OrderByDescending(x => x.CapYear).ToList(); break; case "CapTName": data = (sortDir == "asc") ? data.OrderBy(x => x.CapTName).ToList() : data.OrderByDescending(x => x.CapTName).ToList(); break; case "CapGName": data = (sortDir == "asc") ? data.OrderBy(x => x.CapGName).ToList() : data.OrderByDescending(x => x.CapGName).ToList(); break; case "CapGTName": data = (sortDir == "asc") ? data.OrderBy(x => x.CapGTName).ToList() : data.OrderByDescending(x => x.CapGTName).ToList(); break; } int start = initialPage.HasValue ? (int)initialPage / (int)pageSize : 0; int length = pageSize ?? 10; var list = data.Select((s, i) => new CapabilityViewModel() { RowNumber = ++i, CapID = s.CapID, CapYear = s.CapYear, CapTID = s.CapTID, CapTName = s.CapTName, CapGID = s.CapGID, CapGName = s.CapGName, CapGTID = s.CapGTID, CapGTName = s.CapGTName }).Skip(start * length).Take(length).ToList(); CapabilityResult result = new CapabilityResult(); result.draw = draw ?? 0; result.recordsTotal = recordsTotal; result.recordsFiltered = recordsFiltered; result.data = list; return(result); } }
/// <summary> /// Wrapper to lookup capabilities <see cref="Threema.MsgApi.APIConnector.LookupKeyCapability"/> /// </summary> /// <param name="threemaId">Id for lookup</param> /// <param name="from">Sender id</param> /// <param name="secret">Sender secret</param> /// <param name="apiUrl">Optional api url</param> /// <returns>Array with capatilities</returns> public System.Collections.ArrayList LookupKeyCapability(string threemaId, string from, string secret, string apiUrl = APIConnector.DEFAULTAPIURL) { CapabilityResult capabilities = this.CreateConnector(from, secret, apiUrl) .LookupKeyCapability(threemaId); var result = new ArrayList(); capabilities.Capabilities.ToList().ForEach(c => result.Add(c)); return(result); }
protected override void Execute() { string threemaId = this.threemaIdField.Value; string from = this.fromField.Value; string secret = this.secretField.Value; CapabilityResult capabilities = this.CreateConnector(from, secret) .LookupKeyCapability(threemaId); System.Console.WriteLine(capabilities); }
/// <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)); }
/// <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)); }