public static bool GetAccessControlPrincipal(OAuth2Token token, string principalId, string folderitemid)
        {
            String uri = string.Format("https://{0}/sf/v3/Items({1})/AccessControls", ShareFileV3Sample.GetHostname(token), folderitemid, principalId);

            Console.WriteLine(uri);

            HttpWebRequest request = WebRequest.CreateHttp(uri);

            ShareFileV3Sample.addAuthorizationHeader(request, token);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine(response.StatusCode);
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                String body = reader.ReadToEnd();
                if (body.Contains(principalId))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        /// <summary>
        /// Downloads a single Item.
        /// </summary>
        /// <param name="token">the OAuth2Token returned from Authenticate</param>
        /// <param name="itemId">the id of the item to download</param>
        /// <param name="localPath">where to download the item to, like "c:\\path\\to\\the.file". If downloading a folder the localPath name should end in .zip.</param>
        public static void DownloadItem(OAuth2Token token, string itemId, string localPath)
        {
            String uri = string.Format("https://{0}/sf/v3/Items({1})/Download", ShareFileV3Sample.GetHostname(token), itemId);

            Console.WriteLine(uri);

            HttpWebRequest request = WebRequest.CreateHttp(uri);

            ShareFileV3Sample.addAuthorizationHeader(request, token);
            request.AllowAutoRedirect = true;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (var source = new BufferedStream(response.GetResponseStream()))
            {
                using (var target = new FileStream(localPath, FileMode.Create))
                {
                    int    chunkSize = 1024 * 8;
                    byte[] chunk     = new byte[chunkSize];
                    int    len       = 0;
                    while ((len = source.Read(chunk, 0, chunkSize)) > 0)
                    {
                        target.Write(chunk, 0, len);
                    }
                    Console.WriteLine("Download complete");
                }
            }
            Console.WriteLine(response.StatusCode);
        }
        /// <summary>
        /// Uploads a File using the Standard upload method with a multipart/form mime encoded POST.
        /// </summary>
        /// <param name="token">the OAuth2Token returned from Authenticate</param>
        /// <param name="parentId">where to upload the file</param>
        /// <param name="localPath">the full path of the file to upload, like "c:\\path\\to\\file.name"</param>
        public static void UploadFile(OAuth2Token token, string parentId, string localPath)
        {
            String uri = string.Format("https://{0}/sf/v3/Items({1})/Upload", ShareFileV3Sample.GetHostname(token), parentId);

            Console.WriteLine(uri);

            HttpWebRequest request = WebRequest.CreateHttp(uri);

            ShareFileV3Sample.addAuthorizationHeader(request, token);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                String body = reader.ReadToEnd();

                JObject uploadConfig = JObject.Parse(body);
                string  chunkUri     = (string)uploadConfig["ChunkUri"];
                if (chunkUri != null)
                {
                    Console.WriteLine("Starting Upload");
                    UploadMultiPartFile("File1", new FileInfo(localPath), chunkUri);
                }
            }
        }
        /// <summary>
        /// Update the name and description of an Item.
        /// </summary>
        /// <param name="token">the OAuth2Token returned from Authenticate</param>
        /// <param name="itemId">the id of the item to update</param>
        /// <param name="name">the item name</param>
        /// <param name="description">the item description</param>
        public static void UpdateItem(OAuth2Token token, string itemId, string name, string description)
        {
            String uri = string.Format("https://{0}/sf/v3/Items({1})", ShareFileV3Sample.GetHostname(token), itemId);

            Console.WriteLine(uri);

            HttpWebRequest request = WebRequest.CreateHttp(uri);

            ShareFileV3Sample.addAuthorizationHeader(request, token);

            Dictionary <string, object> folder = new Dictionary <string, object>();

            folder.Add("Name", name);
            folder.Add("Description", description);
            string json = JsonConvert.SerializeObject(folder);

            Console.WriteLine(json);

            request.Method      = "PATCH";
            request.ContentType = "application/json";
            using (var writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(json);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine(response.StatusCode);
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                String  body      = reader.ReadToEnd();
                JObject newFolder = JObject.Parse(body);
                Console.WriteLine("Updated Folder: " + newFolder["Id"]);
            }
        }
        /// <summary>
        /// Get a folder using some of the common query parameters that are available. This will
        /// add the expand, select parameters. The following are used:
        /// expand=Children to get any Children of the folder
        /// select=Id,Name,Children/Id,Children/Name,Children/CreationDate to get the Id, Name of the folder and the Id, Name, CreationDate of any Children
        /// </summary>
        /// <param name="token">the OAuth2Token returned from Authenticate</param>
        /// <param name="id">a folder id</param>
        public static void GetFolderWithQueryParameters(OAuth2Token token, string id)
        {
            String uri = string.Format("https://{0}/sf/v3/Items({1})?$expand=Children&$select=Id,Name,Children/Id,Children/Name,Children/CreationDate", ShareFileV3Sample.GetHostname(token), id);

            Console.WriteLine(uri);

            HttpWebRequest request = WebRequest.CreateHttp(uri);

            ShareFileV3Sample.addAuthorizationHeader(request, token);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine(response.StatusCode);
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                String body = reader.ReadToEnd();

                JObject folder = JObject.Parse(body);
                // only Id and Name are available because we specifically selected only those two Properties
                Console.WriteLine(folder["Id"] + " " + folder["Name"]);
                JArray children = (JArray)folder["Children"];
                if (children != null)
                {
                    foreach (JObject child in children)
                    {
                        // CreationDate is also available on Children because we specifically selected that property in addition to Id, Name
                        Console.WriteLine(child["Id"] + " " + child["CreationDate"] + " " + child["Name"]);
                    }
                }
            }
        }
        /// <summary>
        /// Get the root level Item for the provided user. To retrieve Children the $expand=Children
        /// parameter can be added.
        /// </summary>
        /// <param name="token">the OAuth2Token returned from Authenticate</param>
        /// <param name="getChildren">retrieve Children Items if true, default is false</param>
        public static void GetRoot(OAuth2Token token, bool getChildren = false)
        {
            String uri = string.Format("https://{0}/sf/v3/Items", ShareFileV3Sample.GetHostname(token));

            if (getChildren)
            {
                uri += "?$expand=Children";
            }
            Console.WriteLine(uri);

            HttpWebRequest request = WebRequest.CreateHttp(uri);

            ShareFileV3Sample.addAuthorizationHeader(request, token);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine(response.StatusCode);
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                String body = reader.ReadToEnd();

                JObject root = JObject.Parse(body);

                // just print Id, CreationDate, Name of each element
                Console.WriteLine(root["Id"] + " " + root["CreationDate"] + " " + root["Name"]);
                JArray children = (JArray)root["Children"];
                if (children != null)
                {
                    foreach (JObject child in children)
                    {
                        Console.WriteLine(child["Id"] + " " + child["CreationDate"] + " " + child["Name"]);
                    }
                }
            }
        }
        /// <summary>
        /// Delete an Item by Id.
        /// </summary>
        /// <param name="token">the OAuth2Token returned from Authenticate</param>
        /// <param name="itemId">the id of the item to delete</param>
        public static void DeleteItem(OAuth2Token token, string itemId)
        {
            String uri = string.Format("https://{0}/sf/v3/Items({1})", ShareFileV3Sample.GetHostname(token), itemId);

            Console.WriteLine(uri);

            HttpWebRequest request = WebRequest.CreateHttp(uri);

            ShareFileV3Sample.addAuthorizationHeader(request, token);

            request.Method = "DELETE";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine(response.StatusCode);
        }
        /// <summary>
        /// Get a single Item by Id.
        /// </summary>
        /// <param name="token">the OAuth2Token returned from Authenticate</param>
        /// <param name="id">an item id</param>
        public static void GetItemById(OAuth2Token token, string id)
        {
            String uri = string.Format("https://{0}/sf/v3/Items({1})", ShareFileV3Sample.GetHostname(token), id);

            Console.WriteLine(uri);

            HttpWebRequest request = WebRequest.CreateHttp(uri);

            ShareFileV3Sample.addAuthorizationHeader(request, token);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine(response.StatusCode);
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                String body = reader.ReadToEnd();

                JObject item = JObject.Parse(body);
                Console.WriteLine(item["Id"] + " " + item["CreationDate"] + " " + item["Name"]);
            }
        }
Exemple #9
0
        /// <summary> Function which submits a frame to the Emotion API. </summary>
        /// <param name="frame"> The video frame to submit. </param>
        /// <returns> A <see cref="Task{LiveCameraResult}"/> representing the asynchronous API call,
        ///     and containing the emotions returned by the API. </returns>
        private async Task <LiveCameraResult> AuthorizedFacesFunction(VideoFrame frame)
        {
            // Encode image.
            var jpg = frame.Image.ToMemoryStream(".jpg", s_jpegParams);

            // Submit image to API.
            Face[] faces = null;

            var personNames = new List <string>();
            // See if we have local face detections for this image.
            var localFaces = (OpenCvSharp.Rect[])frame.UserData;

            if (localFaces == null || localFaces.Count() > 0)
            {
                Console.WriteLine("OpenCVSharp found faces and will submit to API");
                Properties.Settings.Default.FaceAPICallCount++;
                faces = await _faceClient.DetectAsync(jpg);

                var faceIds = faces.Select(face => face.FaceId).ToArray();
                if (faceIds.Count() > 0)
                {
                    var results = await _faceClient.IdentifyAsync(personGroupId, faceIds);

                    foreach (var identifyResult in results)
                    {
                        Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                        if (identifyResult.Candidates.Length == 0)
                        {
                            personNames.Add("Suspicious");
                        }
                        else
                        {
                            // Get top 1 among all candidates returned
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person      = await _faceClient.GetPersonAsync(personGroupId, candidateId);

                            Console.WriteLine("Identified as {0}", person.Name);
                            personNames.Add(person.Name);

                            if (person.Name.Contains("Aaron"))
                            {
                                OAuth2Token token     = ShareFileV3Sample.Authenticate(hostname, clientId, clientSecret, username, password);
                                bool        isAllowed = ShareFileV3Sample.GetAccessControlPrincipal(token, AaronPrincipalID, ConfidentialFolderID);
                                if (!isAllowed)
                                {
                                    Security.LockWorkStation();
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Local face detection found no faces; don't call Cognitive Services");
                // Local face detection found no faces; don't call Cognitive Services.
                faces = new Face[0];
            }
            // Output.
            return(new LiveCameraResult
            {
                Faces = faces.Select(e => CreateFace(e.FaceRectangle)).ToArray(), PersonNames = personNames
            });
        }