Esempio n. 1
0
        public bool TrySaveFile(string fileName, IList <INote> notes)
        {
            var stream = BinaryFileUtils.Serialize(notes);

            if (fileName.Length <= 0)
            {
                stream.Dispose();
                return(false);
            }

            try
            {
                using (var fileStream = new FileStream(fileName, FileMode.Create))
                {
                    stream.Position = 0;
                    stream.CopyTo(fileStream);
                }

                _dialogService.ShowMessage("Notebook saved successfully!");
                stream.Dispose();
                return(true);
            }
            catch (SerializationException ex)
            {
                _dialogService.ShowMessage("Failed to serialize. " + ex.Message, "Error", MessageBoxButton.OK,
                                           MessageBoxImage.Error);
                stream.Dispose();
                return(false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// The main conversion API. Calling this function sends an audio file to AT&amp;T service for translation to text.
        /// </summary>
        /// <param name="attachment">Audio filename as it is stored on disk.</param>
        /// <param name="speechContext">Speech content.</param>
        /// <returns>Instance of <see cref="SpeechResponse"/> with sent speech response information.</returns>
        /// <exception cref="System.ArgumentNullException">Throws an exception when attachment is null.</exception>
        public async Task <SpeechResponse> SpeechToText(StorageFile attachment, XSpeechContext speechContext = XSpeechContext.Generic)
        {
            Argument.ExpectNotNull(() => attachment);

            byte[] audioFileBytes = await BinaryFileUtils.ReadAllBytes(attachment);

            var restEndPoint = new Uri(Settings.EndPoint, SendRelativeUrl);

            var    content     = new ByteArrayContent(audioFileBytes);
            string contentType = ContentTypeMapping.MapContentTypeFromExtension(attachment.FileType);

            content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            content.Headers.Add("X-SpeechContext", Enum.GetName(typeof(XSpeechContext), speechContext));

            string strResponse = await SendContentRequest(HttpMethod.Post, restEndPoint.ToString(), content);

            return(SpeechResponse.Parse(strResponse));
        }
Esempio n. 3
0
        private static async Task <byte[]> EncodeBinaryContent(IEnumerable <Windows.Storage.StorageFile> attachments, string textContent, string timeStampStr)
        {
            var encoding = new UTF8Encoding();

            byte[] buffer          = null;
            bool   isFirstFile     = true;
            string currentFileName = String.Empty;

            try
            {
                foreach (var fileName in attachments)
                {
                    currentFileName = fileName.Name;
                    byte[] attachmentPrefixBytes = encoding.GetBytes(CreateAttachmentPrefix(isFirstFile, textContent, fileName.Path, timeStampStr));
                    byte[] fileBytes             = await BinaryFileUtils.ReadAllBytes(fileName);

                    byte[] attachBuffer = BinaryFileUtils.CombineByteArrays(attachmentPrefixBytes, fileBytes);

                    if (isFirstFile)
                    {
                        buffer = attachBuffer;
                    }
                    else
                    {
                        buffer = BinaryFileUtils.CombineByteArrays(buffer, attachBuffer);
                    }

                    isFirstFile = false;
                }
            }
            catch (FileNotFoundException ex)
            {
                throw new FileNotFoundException(string.Format("File '{0}' not found", currentFileName), ex);
            }

            return(BinaryFileUtils.CombineByteArrays(buffer, encoding.GetBytes("\r\n--" + timeStampStr + "--\r\n")));
        }
Esempio n. 4
0
        public bool TryLoadFile(string fileName, IList <INote> notes)
        {
            try
            {
                using (var fileStream = new FileStream(fileName, FileMode.Open))
                {
                    var items = BinaryFileUtils.Deserialize <IList <INote> >(fileStream);
                    ThrowIf.Variable.IsNull(items, nameof(items));

                    var itemsToRemove = notes.ToList();
                    foreach (var item in itemsToRemove)
                    {
                        notes.Remove(item);
                    }
                    foreach (var item in items)
                    {
                        notes.Add(item);
                    }
                }

                _dialogService.ShowMessage("Successfully loaded!");
                return(true);
            }
            catch (SerializationException ex)
            {
                _dialogService.ShowMessage("Failed to deserialize. " + ex.Message, "Error", MessageBoxButton.OK,
                                           MessageBoxImage.Error);
                return(false);
            }
            catch (FileNotFoundException)
            {
                _dialogService.ShowMessage($"File {fileName} doesn't exist", "Error", MessageBoxButton.OK,
                                           MessageBoxImage.Error);
                return(false);
            }
        }