public string GetStrollerImageFile(int id, DownloadFileType fileType) { IStrollerRetriever ret; switch (fileType) { default: return(null); case DownloadFileType.Zip: ret = new StrollerRetrieverZip(); break; case DownloadFileType.Json: ret = new StrollerRetrieverJson(); break; } var image = GetImage(id); if (image == null) { return(null); } var strollerImage = new StrollerFileInfo { CreatedAt = image.CreatedAt, Id = image.Id.ToString(), Name = image.Name, Thumbnail = image.Thumbnail }; var chunks = new List <StrollerChunkItem>(); var chunkManager = new ChunkManager(DbOptions); try { foreach (var imageChunk in image.Chunks) { var chunkRecord = chunkManager.GetChunk(imageChunk.Id); if (!string.IsNullOrEmpty(chunkRecord?.Data)) { chunks.Add(new StrollerChunkItem { Index = chunkRecord.Index, Data = chunkRecord.Data }); } } } catch (Exception e) { throw new ApiException(e.Message, HttpStatusCode.InternalServerError); } strollerImage.Chunks = chunks; return(ret.GetFile(strollerImage)); }
public string GetFile(StrollerFileInfo strollerFileInfo) { var tmpDir = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())); try { if (strollerFileInfo.Chunks.Count == 0) { return(null); } var extension = StringHelper.ExtractFileExtensionFromBase64(strollerFileInfo.Chunks[0].Data); try { Parallel.ForEach(strollerFileInfo.Chunks, (item, state, arg3) => { var data = item.Data; if (data.Contains(",")) { var splits = data.Split(","); if (splits != null && splits.Length > 0) { data = splits[1]; } } var buff = Convert.FromBase64String(data); var fPath = Path.Combine(tmpDir.FullName, item.Index + (string.IsNullOrEmpty(extension) ? string.Empty : "." + extension)); using (var fs = File.Open(fPath, FileMode.CreateNew, FileAccess.Write)) { fs.Write(buff, 0, buff.Length); } }); } catch (Exception e) { throw new ApiException(e.Message, HttpStatusCode.InternalServerError); } var zipPath = Path.Combine(Path.GetTempPath(), tmpDir.Name + ".zip"); using (var zipFile = ZipFile.Open(zipPath, ZipArchiveMode.Create)) { foreach (var file in Directory.GetFiles(tmpDir.FullName)) { zipFile.CreateEntryFromFile(file, Path.GetFileName(file)); } } return(zipPath); } finally { Directory.Delete(tmpDir.FullName, true); } }
public string GetFile(StrollerFileInfo strollerFileInfo) { var json = new JObject(); if (!string.IsNullOrEmpty(strollerFileInfo.Name)) { json["name"] = strollerFileInfo.Name; } if (!string.IsNullOrEmpty(strollerFileInfo.Thumbnail)) { json["thumbnail"] = strollerFileInfo.Thumbnail; } if (strollerFileInfo.CreatedAt != null) { json["createdAt"] = strollerFileInfo.CreatedAt.Value.ToUniversalTime() .ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"); } if (strollerFileInfo.Chunks != null && strollerFileInfo.Chunks.Count > 0) { var chunks = new JArray(); foreach (var strollerChunkItem in strollerFileInfo.Chunks) { var obj = new JObject { ["index"] = strollerChunkItem.Index, ["image"] = strollerChunkItem.Data }; chunks.Add(obj); } json["chunks"] = chunks; } var tmpFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".json"); var jsonStrin = JsonConvert.SerializeObject(json); File.WriteAllText(tmpFilePath, jsonStrin); return(tmpFilePath); }
public Image AddImage(StrollerFileInfo fInfo) { if (fInfo == null) { throw new ArgumentNullException(nameof(fInfo), "Stroler file data was not provided"); } var image = new Image { ChunkWidth = 0, ChunkHeight = 0, Thumbnail = fInfo.Thumbnail, Name = fInfo.Name }; image = AddImage(image); var chunksManager = new ChunkManager(DbOptions); try { Parallel.ForEach(fInfo.Chunks, (item, state, arg3) => { chunksManager.AddChunk(new Chunk { Data = item.Data, Index = item.Index }, image.Id); }); } catch (Exception ex) { throw new ApiException("Unable to add one or more chunks: " + ex.Message, ex, HttpStatusCode.BadRequest); } return(image); }
public static StrollerFileInfo JsonStreamToStrollerFileInfo(Stream fileStream, string fileName) { if (fileStream == null) { throw new ApiException("Provided file stream is empty", HttpStatusCode.BadRequest); } string jsonString; try { using (var sr = new StreamReader(fileStream)) { jsonString = sr.ReadToEnd(); } } catch (Exception ex) { throw new ApiException("Error occurred while reading input file stream: " + ex.Message, ex, HttpStatusCode.BadRequest); } JObject json; try { json = JsonConvert.DeserializeObject(jsonString) as JObject; } catch (Exception exception) { throw new ApiException("Error occurred while converting input file to JSON object: " + exception.Message, exception, HttpStatusCode.BadRequest); } if (json == null) { throw new ApiException("Invalid JSON file format", HttpStatusCode.BadRequest); } if (json["chunks"] == null || json["chunks"].GetType() != typeof(JArray)) { throw new ApiException("Invalid Stroller file format. No chunks collection found", HttpStatusCode.BadRequest); } var chunks = (JArray)json["chunks"]; foreach (var job in chunks) { if (job == null || job.GetType() != typeof(JObject)) { throw new ApiException( "Invalid Stroller file structure. Chunks array does not contain proper fields", HttpStatusCode.BadRequest); } if (job["index"] == null) { throw new ApiException( "Invalid Stroller file structure. Some chunks do not contain index number", HttpStatusCode.BadRequest); } if (!int.TryParse(job["index"].ToString(), out _)) { throw new ApiException( "Invalid Stroller file structure. Invalid index number for some chunks. Could not extract index value", HttpStatusCode.BadRequest); } if (job["image"] == null) { throw new ApiException( "Invalid Stroller file structure. Some chunks do not contain image data", HttpStatusCode.BadRequest); } } var chunkItems = new List <StrollerChunkItem>(); foreach (var chunk in chunks) { chunkItems.Add(new StrollerChunkItem { Data = chunk["image"].ToString(), Index = int.Parse(chunk["index"].ToString()) }); } var result = new StrollerFileInfo { Chunks = chunkItems }; if (json["thumbnail"] != null) { result.Thumbnail = json["thumbnail"].ToString(); } if (json["createdAt"] != null) { DateTime dt; if (DateTime.TryParse(json["createdAt"].ToString(), out dt)) { result.CreatedAt = dt; } } if (string.IsNullOrEmpty(fileName)) { return(result); } fileName = Path.GetFileNameWithoutExtension(fileName); var parts = fileName.Split(' ', '-', '_'); if (parts.Length <= 0) { return(result); } for (var i = 0; i < parts.Length; i++) { if (i == 0) { parts[i] = parts[i].First().ToString().ToUpper() + parts[i].Substring(1); } } var name = string.Join(' ', parts); result.Name = name; return(result); }