I'm using CouchApp to build an easy web application that allows to upload and manage pictures. The actual image file is stored as attachment to the doc like show below.
{
"_id":"09fe82d75a26f9aa5e722d6b220180d2",
"_rev":"2-5797b822c83b9d41545139caa592f611",
"data":"some additional fields with info about the image",
"_attachments":
{
"foo.jpg":
{
"stub":true,
"content_type":"image/jpeg",
"length":23721
}
}
}
But for integrating the image in html i need the url to the attachment. How do i get this url?
I'm using evently and mustache for generating the web pages. Below is the data.js for reading the data:
function(data) {
return {
items : data.rows.map(function(r) {
return {
id : r.value._id,
rev : r.value._rev,
title : r.value.description,
url : "how am i supposed to do this?"
};
})
};
};
The URL to the attachment would be http://domain/database/09fe82d75a26f9aa5e722d6b220180d2/foo.jpg
If your filenames are dynamic, you would have to iterate the _attachments object and fetch the keys on your way - that's where your filename would be.
for(var filename in r.value._attachments){break;}
// ...
url : '<database>/' + r.value._id +'/'+filename;
Related
I'm on a channel that sends messages that contains a text, with a link (that link has not an image) and an image (tip product from amazon):
I tried with this code and it's similar:
bot.telegram.sendMessage('mychannel', `Hello https:/path/to/image.jpg`)
And it works it similar, but it remains the link. SO how can i put that way with image preview but not the link?
Thank you
One workaround(trick) would be to insert the link but use an empty character unicode (like from https://emptycharacter.com/)
Here is an example (I inserted the photo URL with an empty character)
some sample code to get you started:
const Telegraf = require("telegraf");
const bot = new Telegraf(" ... ");
const CHAT_ID = ... ;
function getHiddenLink(url, parse_mode = "markdown") {
const emptyChar = ""; // copied and pasted the char from https://emptycharacter.com/
switch (parse_mode) {
case "markdown":
return `[${emptyChar}](${url})`;
case "HTML":
return `${emptyChar}`;
default:
throw new Error("invalid parse_mode");
}
}
// Option 1: sending with MARKDOWN syntax
bot.telegram.sendMessage(
CHAT_ID,
`
some test text in markdown
${getHiddenLink("https://i.stack.imgur.com/TPKR5.png", "markdown")}
`,
{
parse_mode: "markdown",
}
);
// Another option: sending with HTML syntax
bot.telegram.sendMessage(
CHAT_ID,
`
some test text in HTML
${getHiddenLink("https://i.stack.imgur.com/TPKR5.png", "HTML")}
`,
{
parse_mode: "HTML",
}
);
Here we just create a new function getHiddenLink() that accepts the URL and parse_mode (HTML or markdown) and just craft a new URL representation with the empty character as the link-text and return it.
There is no way to send a preview without a link. You can try to have some by your own telegram account and you will see it is impossible.
Instead of preview, add some caption to photos.
I am working on a simple nodejs console utility that will upload images for the training of a Custom Vision model. I do this mainly because the customvision web app won't let you tag multiple images at once.
tl;dr: How to post images into the CreateImagesFromFiles API endpoint?
I cannot figure out how to pass images that I want to upload. The documentation just defines a string as a type for one of the properties (content I guess). I tried passing path to local file, url to online file and even base64 encoded image as a string. Nothing passed.
They got a testing console (blue button "Open API testing console" at the linked docs page) but once again... it's vague and won't tell you what kind of data it actually expects.
The code here isn't that relevant, but maybe it helps...
const options = {
host: 'southcentralus.api.cognitive.microsoft.com',
path: `/customvision/v2.0/Training/projects/${projectId}/images/files`,
method: 'POST',
headers: {
'Training-Key': trainingKey,
'Content-Type': 'application/json'
}
};
const data = {
images: [
{
name: 'xxx',
contents: 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAEklEQVR42mP8z8AARKiAkQaCAFxlCfyG/gCwAAAAAElFTkSuQmCC',
tagIds: [],
regions: []
}
],
tagIds: []
}
const req = http.request(options, res => {
...
})
req.write(JSON.stringify(data));
req.end();
Response:
BODY: { "statusCode": 404, "message": "Resource not found" }
No more data in response.
I got it working using the "API testing console" feature, so I can help you to identify your issue (but sorry, I'm not expert in node.js so I will guide you with C# code)
Format of content for API
You are right, the documentation is not clear about the content the API is waiting for. I made some search and found a project in a Microsoft's Github repository called Cognitive-CustomVision-Windows, here.
What is saw is that they use a class called ImageFileCreateEntry whose signature is visible here:
public ImageFileCreateEntry(string name = default(string), byte[] contents = default(byte[]), IList<System.Guid> tagIds = default(IList<System.Guid>))
So I guessed it's using a byte[].
You can also see in their sample how they did for this "batch" mode:
// Or uploaded in a single batch
var imageFiles = japaneseCherryImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList();
trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List<Guid>() { japaneseCherryTag.Id }));
Then this byte array is serialized with Newtonsoft.Json: if you look at their documentation (here) it says that byte[] are converted to String (base 64 encoded). That's our target.
Implementation
As you mentioned that you tried with base64 encoded image, I gave it a try to check. I took my StackOverflow profile picture that I downloaded locally. Then using the following, I got the base64 encoded string:
Image img = Image.FromFile(#"\\Mac\Home\Downloads\Picto.jpg");
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
arr = ms.ToArray();
}
var content = Convert.ToBase64String(arr);
Later on, I called the API with no tags to ensure that the image is posted and visible:
POST https://southcentralus.api.cognitive.microsoft.com/customvision/v2.2/Training/projects/MY_PROJECT_ID/images/files HTTP/1.1
Host: southcentralus.api.cognitive.microsoft.com
Training-Key: MY_OWN_TRAINING_KEY
Content-Type: application/json
{
"images": [
{
"name": "imageSentByApi",
"contents": "/9j/4AAQSkZJRgA...TOO LONG FOR STACK OVERFLOW...",
"tagIds": [],
"regions": []
}
],
"tagIds": []
}
Response received: 200 OK
{
"isBatchSuccessful": true,
"images": [{
"sourceUrl": "imageSentByApi",
"status": "OK",
"image": {
"id": "GENERATED_ID_OF_IMAGE",
"created": "2018-11-05T22:33:31.6513607",
"width": 328,
"height": 328,
"resizedImageUri": "https://irisscuprodstore.blob.core.windows.net/...",
"thumbnailUri": "https://irisscuprodstore.blob.core.windows.net/...",
"originalImageUri": "https://irisscuprodstore.blob.core.windows.net/..."
}
}]
}
And my image is here in Custom Vision portal!
Debugging your code
In order to debug, you should 1st try to submit your content again with tagIds and regions arrays empty like in my test, then provide the content of the API reply
I need to get an image URL from Contentful entry id.
I am getting such an JSON from Contentful query
{
"sys":{
"space":{
"sys":{
"type":"Link",
"linkType":"Space",
"id":"8v1e7eaw70p2"
}
},
"id":"1JfEwVlD9WmYikE8kS8iCA",
"type":"Entry",
"createdAt":"2018-02-28T18:50:08.758Z",
"updatedAt":"2018-02-28T18:50:08.758Z",
"revision":1,
"contentType":{
"sys":{
"type":"Link",
"linkType":"ContentType",
"id":"image"
}
},
"locale":"en-US"
},
"fields":{
"name":"heat",
"image":{
"sys":{
"type":"Link",
"linkType":"Asset",
"id":"6Inruq2U0M2kOYsSAu8Ywk"
}
}
}
}
I am using JS driver they provide:
client.getEntry()
so how to go thru that link: 6Inruq2U0M2kOYsSAu8Ywk ?
Unfortunately, the js SDK will not be able to resolve links when using the single entry endpoint i.e client.getEntry() because there won't be enough data.
When thing I always recommend to work around this is to use the collection endpoint with a query the desired id as a query param. This way you will always get the desired entry with all it's linked data.
Your code should look something like this
client.getEntries({'sys.id': '6Inruq2U0M2kOYsSAu8Ywk'})
.then(response => console.log(response.items[0].fields.image.fields.file.url))
I hope that helps.
Best,
Khaled
Use client.getEntries({'sys.id': '1JfEwVlD9WmYikE8kS8iCA'})
To get the entry fields and the asset fields.
You can also patch the assets to the fields by running this after fetching the data:
/* Patch all the assets to the fields */
const patchAssets = (fields, assets) => {
Object.keys(fields).forEach(function (key) {
let obj = fields[key];
if (obj.sys && obj.sys.linkType === 'Asset') {
const assetId = obj.sys.id;
const matchAsset = assets.find(asset => {
return asset.id === assetId;
});
obj.file = matchAsset;
}
});
return fields;
};
Another way to get image url is to use getAsset('<asset_id>'). So first, using the getEntry() method, you need to get the entry data, then extract the id from the field: fields.image.sys.id, and pass it to the getAsset method.
I am creating a blog with Node, Express and MongoDB. I'm using Mongoose to connect to MongoDB.
I have a create new post form that creates and saves new posts in MongoDB just fine.
When creating a post you can mark the post as published or leave that option unchecked. When you save the post I want you to either:
A) Be redirected to the home page if the post was published, or
B) be redirected to the post's edit/update page if the post was not marked to be published.
Here's the code in the view that I'm trying to use to accomplish the above:
addPost: function(req, res) {
return new Post(req.body.post).save(function() {
if (req.body.published === true) {
return res.redirect("/");
} else {
return res.redirect("/office/post/" + [NEED OBJECT ID HERE] + "/edit");
}
});
}
This is the corresponding view that sends the POST data:
form.web-form(method="post", action="/post/new")
fieldset.fieldset
label.form-label(for="title") Title
input.text-input(id="title", type="text", name="post[title]", placeholder="Post title")
input.text-input(id="alias", type="hidden", name="post[alias]")
label.form-label(for="subhead") Subhead
input.text-input(id="subhead", type="text", name="post[subhead]", placeholder="Post subhead")
label.form-label(for="preview") Preview
textarea.text-area(id="preview", name="post[preview]", rows="4", placeholder="Preview")
label.form-label(for="post-body") Body
textarea.text-area(id="post-body", name="post[body]", rows="5", placeholder="Main content")
input.check-box(onclick="changeButton()", id="published", type="checkbox", name="post[published]")
label.inline-label(for="published") Publish
input.btn-submit(id="submit-post", type="submit", value="Save!")
a.btn-cancel(href="/") Cancel
Any help is greatly appreciated! Thanks!
Like this?
addPost: function(req, res) {
// strip 'post[' and ']' from submitted parameters
var params = {};
for (var k in req.body)
{
params[k.substring(5, k.length - 1)] = req.body[k];
};
var post = new Post(params);
return post.save(function() {
if (params.published === true) {
return res.redirect("/");
} else {
return res.redirect("/office/post/" + post._id + "/edit");
}
});
}
Before posting comments a message is shown:
Login or register to post comments
I want to modify the output of the 2 links "login" and "register", mainly I want to add some classes to the links to format it nicely with some img. buttons of different colors
I really need to "tell" the output to put and , and by default doesn't have no class....
I know it can be done with some hook or something but I can't find no info about that ...
The piece responsible for that line is locate in theme_comment_post_forbidden in drupal_root/modules/comment/comment.module
it looks like this
function theme_comment_post_forbidden($node) {
global $user;
static $authenticated_post_comments;
if (!$user->uid) {
if (!isset($authenticated_post_comments)) {
// We only output any link if we are certain, that users get permission
// to post comments by logging in. We also locally cache this information.
$authenticated_post_comments = array_key_exists(DRUPAL_AUTHENTICATED_RID, user_roles(TRUE, 'post comments') + user_roles(TRUE, 'post comments without approval'));
}
if ($authenticated_post_comments) {
// We cannot use drupal_get_destination() because these links
// sometimes appear on /node and taxonomy listing pages.
if (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_SEPARATE_PAGE) {
$destination = 'destination='. rawurlencode("comment/reply/$node->nid#comment-form");
}
else {
$destination = 'destination='. rawurlencode("node/$node->nid#comment-form");
}
if (variable_get('user_register', 1)) {
// Users can register themselves.
return t('Login or register to post comments', array('#login' => url('user/login', array('query' => $destination)), '#register' => url('user/register', array('query' => $destination))));
}
else {
// Only admins can add new users, no public registration.
return t('Login to post comments', array('#login' => url('user/login', array('query' => $destination))));
}
}
}
}
I suggest modifying your theme's template.php and adding a new function phptemplate_comment_post_forbidden($node) where you'll copy the contents of theme_comment_post_comments and do the necessary modifications.