Esempio n. 1
0
 public static Image FromStream(Stream stream, string filename)
 {
     byte[] fileBytes = stream.ReadAllBytes();
     return new Image
     {
         ContentType = MimeUtility.GetMimeType(fileBytes),
         Data = fileBytes,
         Name = filename,
         Size = stream.Length
     };
 }
        public TranslationModel CreateModel(string baseModelId, string name = null, System.IO.Stream forcedGlossary = null, System.IO.Stream parallelCorpus = null, Stream monolingualCorpus = null)
        {
            if (string.IsNullOrEmpty(baseModelId))
            {
                throw new ArgumentNullException(nameof(baseModelId));
            }

            try
            {
                var formData = new MultipartFormDataContent();

                if (forcedGlossary != null)
                {
                    var forcedGlossaryContent = new ByteArrayContent(forcedGlossary.ReadAllBytes());
                    MediaTypeHeaderValue contentType;
                    MediaTypeHeaderValue.TryParse("application/octet-stream", out contentType);
                    forcedGlossaryContent.Headers.ContentType = contentType;
                    formData.Add(forcedGlossaryContent, "forced_glossary", "filename");
                }

                if (parallelCorpus != null)
                {
                    var parallelCorpusContent = new ByteArrayContent(parallelCorpus.ReadAllBytes());
                    MediaTypeHeaderValue contentType;
                    MediaTypeHeaderValue.TryParse("application/octet-stream", out contentType);
                    parallelCorpusContent.Headers.ContentType = contentType;
                    formData.Add(parallelCorpusContent, "parallel_corpus", "filename");
                }

                if (monolingualCorpus != null)
                {
                    var monolingualCorpusContent = new ByteArrayContent(monolingualCorpus.ReadAllBytes());
                    MediaTypeHeaderValue contentType;
                    MediaTypeHeaderValue.TryParse("text/plain", out contentType);
                    monolingualCorpusContent.Headers.ContentType = contentType;
                    formData.Add(monolingualCorpusContent, "monolingual_corpus", "filename");
                }

                var result = RepositoryClient.WithAuthentication(ApiKeys.LanguageTranslatorUsername, ApiKeys.LanguageTranslatorPassword)
                             .PostAsync($"{ApiKeys.LanguageTranslatorEndpoint}{modelsUrl}")
                             .WithArgument("base_model_id", baseModelId)
                             .WithArgument("name", name)
                             .WithBodyContent(formData)
                             .As <TranslationModel>()
                             .Result;

                return(result);
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }
        }
        /// <summary>
        ///     Writes a stream to a file.
        /// </summary>
        /// <param name="stream">
        ///     The source <see cref="Stream" /> to read from.
        /// </param>
        /// <param name="path">
        ///     The destination path.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="path" /> is <c>null</c>.
        /// </exception>
        public static void WriteToFile([NotNull] this Stream stream, [NotNull] string path)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            var bytes = stream.ReadAllBytes();

            File.WriteAllBytes(path, bytes);
        }
Esempio n. 4
0
 internal static uint Calculate(Stream stream)
 {
     // Copy the stream to a memory steam and get the CRC32 of the bytes
     byte[] content = stream.ReadAllBytes();
     return Calculate(content);
 }