Exemple #1
0
        /// <summary>
        /// Submitting picture, containing textual content, to plagiarism scan
        /// </summary>
        /// <param name="localfile">The local picture containing the content to scan</param>
        /// <param name="language">Specify the language of the text</param>
        /// <param name="options">Process Options: include http callback and add custom fields to the process</param>
        /// <exception cref="UnauthorizedAccessException">The login-token is undefined or expired</exception>
        /// <returns>The newly created process</returns>
        public CopyleaksProcess CreateByOcr(FileInfo localfile, OcrLanguage language, ProcessOptions options = null)
        {
            if (this.Token == null)
            {
                throw new UnauthorizedAccessException("Empty token!");
            }
            else
            {
                this.Token.Validate();
            }

            if (!localfile.Exists)
            {
                throw new FileNotFoundException("File not found!", localfile.FullName);
            }

            if (language == null)
            {
                throw new ArgumentNullException(nameof(language), "Cannot be null!");
            }

            using (HttpClient client = new HttpClient())
            {
                client.SetCopyleaksClient(HttpContentTypes.Json, this.Token);

                if (options != null)
                {
                    options.AddHeaders(client);
                }

                HttpResponseMessage msg;
                // Uploading the local file to the server

                using (var content = new MultipartFormDataContent("Upload----" + DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)))
                    using (FileStream stream = localfile.OpenRead())
                    {
                        content.Add(new StreamContent(stream, (int)stream.Length), "document", Path.GetFileName(localfile.Name));
                        msg = client.PostAsync(
                            string.Format("{0}/{1}/create-by-file-ocr?language={2}", Resources.ServiceVersion, Resources.ServicePage, Uri.EscapeDataString(language.Type.ToString())),
                            content).Result;
                    }

                if (!msg.IsSuccessStatusCode)
                {
                    throw new CommandFailedException(msg);
                }

                string json = msg.Content.ReadAsStringAsync().Result;

                var dateTimeConverter = new IsoDateTimeConverter {
                    DateTimeFormat = "dd/MM/yyyy HH:mm:ss"
                };
                CreateResourceResponse response = JsonConvert.DeserializeObject <CreateResourceResponse>(json, dateTimeConverter);
                if (options == null)
                {
                    return(new CopyleaksProcess(this.Token, response, null));
                }
                else
                {
                    return(new CopyleaksProcess(this.Token, response, options.CustomFields));
                }
            }
        }