Example #1
0
        /// <summary>
        /// Submitting text to plagiarism scan
        /// </summary>
        /// <param name="text">Text to be scanned. The text MUST be encoded with UTF-8.</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>
        /// <exception cref="ArgumentOutOfRangeException">The input URL scheme is different than HTTP or HTTPS</exception>
        /// <returns>The newly created process</returns>
        public CopyleaksProcess CreateByText(string text, ProcessOptions options = null)
        {
            if (this.Token == null)
            {
                throw new UnauthorizedAccessException("Empty token!");
            }
            else
            {
                this.Token.Validate();
            }

            if (text.Trim() == "")
            {
                throw new ArgumentOutOfRangeException(nameof(text), "Empty text not allowed.");
            }

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

                HttpResponseMessage msg;
                HttpContent         content = new StringContent(text, Encoding.UTF8, HttpContentTypes.PlainText);

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

                msg = client.PostAsync(string.Format("{0}/{1}/{2}", Resources.ServiceVersion, this.Product.ToName(), "create-by-text"), content).Result;

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

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

                CreateResourceResponse response;
                try
                {
                    var dateTimeConverter = new IsoDateTimeConverter {
                        DateTimeFormat = "dd/MM/yyyy HH:mm:ss"
                    };
                    response = JsonConvert.DeserializeObject <CreateResourceResponse>(json, dateTimeConverter);
                }
                catch (Exception e)
                {
                    throw new Exception("JSON=" + json, e);
                }
                if (options == null)
                {
                    return(new CopyleaksProcess(this.Token, this.Product, response, null));
                }
                else
                {
                    return(new CopyleaksProcess(this.Token, this.Product, response, options.CustomFields));
                }
            }
        }
Example #2
0
        /// <summary>
        /// Submitting local file to plagiarism scan
        /// </summary>
        /// <param name="localfile">Local file containing the content to scan</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 CreateByFile(FileInfo localfile, 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);
            }

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

                client.Timeout = TimeSpan.FromMinutes(10);                 // Uploading large file may take a while

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

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

                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", Resources.ServiceVersion, this.Product.ToName()), 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, this.Product, response, null));
                }
                else
                {
                    return(new CopyleaksProcess(this.Token, this.Product, response, options.CustomFields));
                }
            }
        }
Example #3
0
        public CreateMultipleResourcesResponse CreateByFiles(ProcessOptions options, params FileInfo[] files)
        {
            if (this.Token == null)
            {
                throw new UnauthorizedAccessException("Empty token!");
            }
            else
            {
                this.Token.Validate();
            }

            string json;

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

                client.Timeout = TimeSpan.FromMinutes(10);                 // Uploading large file may take a while

                HttpResponseMessage msg;

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

                using (var content = new MultipartFormDataContent("Upload----" + DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)))
                {
                    string filename;
                    int    counter = 1;
                    foreach (var file in files)
                    {
                        filename = Path.GetFileName(file.FullName);
                        content.Add(new StreamContent(file.OpenRead()), "document_" + (++counter), filename);
                    }
                    msg = client.PostAsync(string.Format("v2/{0}/create-by-file", this.Product.ToName()), content).Result;
                }

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

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

            var dateTimeConverter = new IsoDateTimeConverter {
                DateTimeFormat = "dd/MM/yyyy HH:mm:ss"
            };
            var data = JsonConvert.DeserializeObject <InnerCreateMultipleResourcesResponse>(json, dateTimeConverter);

            CopyleaksProcess[] processes = new CopyleaksProcess[data.Success.Length];
            for (int i = 0; i < data.Success.Length; ++i)
            {
                processes[i] = new CopyleaksProcess(this.Token, this.Product, data.Success[i], null);
            }

            return(new CreateMultipleResourcesResponse()
            {
                Success = processes,
                Errors = data.Errors
            });
        }
Example #4
0
        /// <summary>
        /// Submitting URL to plagiarism scan
        /// </summary>
        /// <param name="url">The url containing the content to scan</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>
        /// <exception cref="ArgumentOutOfRangeException">The input URL scheme is different than HTTP or HTTPS</exception>
        /// <returns>The newly created process</returns>
        public CopyleaksProcess CreateByUrl(Uri url, ProcessOptions options = null)
        {
            if (this.Token == null)
            {
                throw new UnauthorizedAccessException("Empty token!");
            }
            else
            {
                this.Token.Validate();
            }

            if (url.Scheme != "http" && url.Scheme != "https")
            {
                throw new ArgumentOutOfRangeException(nameof(url), "Allowed protocols: HTTP, HTTPS");
            }

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

                CreateCommandRequest req = new CreateCommandRequest()
                {
                    URL = url.AbsoluteUri
                };

                HttpResponseMessage msg;
                // Submitting the URL
                HttpContent content = new StringContent(
                    JsonConvert.SerializeObject(req),
                    Encoding.UTF8,
                    HttpContentTypes.Json);

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

                msg = client.PostAsync(string.Format("{0}/{1}/{2}", Resources.ServiceVersion, this.Product.ToName(), "create-by-url"), content).Result;

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

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

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