public async Task <IActionResult> Upload() { try { var file = Request.Form.Files[0]; if (file.Length > 0 && file.ContentType.Contains("image")) { using (var stream = new MemoryStream()) { file.CopyTo(stream); var bytes = stream.ToArray(); var image = new MongoImage { Data = bytes, FileType = file.ContentType }; var img = await cont.Create(image); return(Ok(img._id.ToString())); } } else { return(BadRequest()); } } catch (Exception ex) { return(StatusCode(500, "Internal server error")); } }
public override Image LoadImage(string url, HttpRequestParameters_v1 requestParameters = null) { MongoImage mongoImage = LoadMongoImage(url, requestParameters); if (mongoImage.Image == null) { LoadImage(mongoImage); } return(mongoImage.Image); }
protected MongoImage CreateMongoImage(string url, HttpRequestParameters_v1 requestParameters = null) { string file = _urlCache.GetUrlSubPath(url, requestParameters); string path = zPath.Combine(_urlCache.CacheDirectory, file); if (!zFile.Exists(path)) { //Http2.LoadToFile(url, path, requestParameters); Http_v3.LoadToFile(url, path, requestParameters); } Image image = null; if (zFile.Exists(path)) { try { image = zimg.LoadFromFile(path); } catch (Exception exception) { Trace.WriteLine("error unable to load image url \"{0}\" to \"{1}\" (WebImageMongoCacheManager.CreateMongoImage())", url, path); Trace.Write("error : "); Trace.WriteLine(exception.Message); } } else { Trace.WriteLine("error unable to load image url \"{0}\" to \"{1}\" (WebImageMongoCacheManager.CreateMongoImage())", url, path); } MongoImage mongoImage = new MongoImage(); mongoImage.Url = url; mongoImage.File = file; mongoImage.Width = image != null ? image.Width : 0; mongoImage.Height = image != null ? image.Height : 0; mongoImage.Image = image; GetCollection().zUpdate(new QueryDocument { { "_id", BsonValue.Create(url) } }, new UpdateDocument { { "$set", mongoImage.ToBsonDocument() } }, UpdateFlags.Upsert); return(mongoImage); }
protected MongoImage CreateMongoImage(string url, HttpRequestParameters_v1 requestParameters = null) { string file = _urlCache.GetUrlSubPath(url, requestParameters); string path = zPath.Combine(_urlCache.CacheDirectory, file); if (!zFile.Exists(path)) //Http2.LoadToFile(url, path, requestParameters); Http_v3.LoadToFile(url, path, requestParameters); Image image = null; if (zFile.Exists(path)) { try { image = zimg.LoadFromFile(path); } catch (Exception exception) { Trace.WriteLine("error unable to load image url \"{0}\" to \"{1}\" (WebImageMongoCacheManager.CreateMongoImage())", url, path); Trace.Write("error : "); Trace.WriteLine(exception.Message); } } else { Trace.WriteLine("error unable to load image url \"{0}\" to \"{1}\" (WebImageMongoCacheManager.CreateMongoImage())", url, path); } MongoImage mongoImage = new MongoImage(); mongoImage.Url = url; mongoImage.File = file; mongoImage.Width = image != null ? image.Width : 0; mongoImage.Height = image != null ? image.Height : 0; mongoImage.Image = image; GetCollection().zUpdate(new QueryDocument { { "_id", BsonValue.Create(url) } }, new UpdateDocument { { "$set", mongoImage.ToBsonDocument() } }, UpdateFlags.Upsert); return mongoImage; }
public void LoadImage(MongoImage mongoImage) { mongoImage.Image = zimg.LoadFromFile(zPath.Combine(_urlCache.CacheDirectory, mongoImage.File)); }
public void LoadImage(MongoImage mongoImage) { mongoImage.Image = zimg.LoadFromFile(zPath.Combine(_urlCache.CacheDirectory, mongoImage.File)); }