public async Task <string> FileAsync(IFormFile file) { if (file != null) { if (file.Length > 35000000) { throw new UserFriendlyException("Subtitles file too big!"); } else if (file.Length > 0) { try { var stream = file.OpenReadStream(); var words = _parser.ParseUnigueWordsInFile(new Material(), new StreamReader(stream)); var account = await GetCurrentUserOrNullAsync(); var vocabWords = await _vocabularyService.GetSpecifiedVocabWordsAsync(words, account); if (words.Count > 0) { var response = new ParseResponseDto { Words = words, VocabWords = vocabWords }; return(ApiJsonSerializer.Serialize(response)); } } catch (Exception ex) { Trace.TraceError(ex.ToString()); throw new UserFriendlyException("Server error. Please try again later."); } } } else { throw new UserFriendlyException("Empty subtitles file!"); } throw new UserFriendlyException("Server error. Please try again later."); }
/// <summary>Creates material with the specified attributes.</summary> /// <param name="subtitlesStream">Subtitles file stream.</param> /// <param name="imageStream">Image file stream.</param> /// <param name="mimeType">Mime type of image.</param> /// <param name="type">Material type <see cref="MaterialType"/>.</param> /// <param name="name">Name of the material.</param> /// <param name="description">Description of the material.</param> /// <param name="userId">Owner Id.</param> /// <param name="width">Material image width.</param> /// <param name="height">Material image height.</param> /// <returns>Created material by specific attributes.</returns> public Material CreateMaterial(Stream subtitlesStream, Stream imageStream, string mimeType, MaterialType type, string name, string description, int userId, int width, int height) { //TODO: this material exist check var material = new Material { Description = description, Name = name, Type = type, MimeType = mimeType }; using (var streamReader = new StreamReader(subtitlesStream, Encoding.GetEncoding("Windows-1251"))) { var file = new File { Path = userId.ToString(), Filename = name, Extension = type.ToString(), FullName = userId + name + type }; file.Words = _parser.ParseUnigueWordsInFile(file, streamReader); material.File = file; } material.Image = _imageService.CropImage(imageStream, width, height); material.Owner = _accountService.GetByExternalId(userId); return(material); }