Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2021-02-18 21:45:35
- saccade
- Plugin Author
- From: Neubeuern, Germany
- Registered: 2004-11-05
- Posts: 521
importing files into textpattern from a url within a plugin
Hi, I import event-data from a database into textpattern on a regular basis using xml as exchange format.
Within the xml data there are links to images,
is there a plugin oder code snippet how to import an image into textpattern automatically, if you simply provide the url to that image?
Offline
Re: importing files into textpattern from a url within a plugin
A bit of a wild-guess idea: what if you hooked up smd_xml and smd_query: smd_xml to read the relevant part of the xml file, and smd_query to write the relevant data to the database (see Example 6 in Stef’s smd_xml docs). You’d need to design it carefully to guard against errors in incoming data before insertion into the database.
I’m not sure, though, that that would solve the actual bringing in of the file into textpattern. For that you may need a custom plugin to curl in the image from the other location, or alternatively, the image remains where it was and an (as yet non-existant) image-counterpart to smd_remote_file that would allow you to manage links to remote images directly from Textpattern’s images tab, as if they were native…
Another very loose idea, as I have no idea of your situation: if your other database is also online as a mySQL database, rah_swap allows you to switch database connections mid-page. You can use that with smd_query to query the other database directly and output the results in your textpattern page.
TXP Builders – finely-crafted code, design and txp
Offline
Re: importing files into textpattern from a url within a plugin
saccade wrote #328970:
is there a plugin oder code snippet how to import an image into textpattern automatically, if you simply provide the url to that image?
If ‘provide’ means ‘paste the url into input box’, you can try this:
# Name: etc_import v0.3
# Type: Admin/AJAX plugin
#
# Author: Oleg Loukianov
# URL:
# Recommended load order: 5
# .....................................................................
# This is a plugin for Textpattern CMS - http://textpattern.com/
# To install: textpattern > admin > plugins
# Paste the following text into the 'Install plugin' box:
# .....................................................................
YToxMTp7czo0OiJuYW1lIjtzOjEwOiJldGNfaW1wb3J0IjtzOjY6ImF1dGhvciI7czoxNDoi
T2xlZyBMb3VraWFub3YiO3M6MTA6ImF1dGhvcl91cmkiO3M6MDoiIjtzOjc6InZlcnNpb24i
O3M6MzoiMC4zIjtzOjExOiJkZXNjcmlwdGlvbiI7czowOiIiO3M6NDoiY29kZSI7czoyODk3
OiJpZiA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Agbi50ZXh0X2FyZWEoYXJyYXko
CiAgICAgICAgICAnbmFtZScgICAgID0+ICdpbXBvcnQnLAogICAgICAgICAgJ3JlcXVpcmVk
JyA9PiB0cnVlLAogICAgICAgICAgJ2lkJyAgICAgICA9PiAnZXRjX2ltcG9ydCcsCiAgICAg
ICAgICAncGxhY2Vob2xkZXInID0+IGdUeHQoJ3VybCcpLAogICAgICAgICAgJ3Jvd3MnID0+
IDMKICAgICAgKSkubi4KICAgICAgZklucHV0KCdzdWJtaXQnLCAnJywgZ1R4dCgnaW1wb3J0
JykpLAogICAgICAnZm9ybScsCiAgICAgIGFycmF5KAogICAgICAgICAgJ2NsYXNzJyAgID0+
ICd1cGxvYWQtZm9ybScsCiAgICAgICAgICAnbWV0aG9kJyAgPT4gJ3Bvc3QnLAogICAgICAg
ICAgJ2FjdGlvbicgID0+ICJpbmRleC5waHA/ZXZlbnQ9JGV2ZW50IiwKICAgICAgICAgICdv
bnN1Ym1pdCcgPT4gJ2RvY3VtZW50LmdldEVsZW1lbnRzQnlUYWdOYW1lKCJib2R5IilbMF0u
Y2xhc3NOYW1lPSJkaXNhYmxlZCInCiAgICAgICkKICApOwp9CiI7czo0OiJ0eXBlIjtzOjE6
IjQiO3M6NToib3JkZXIiO3M6MToiNSI7czo1OiJmbGFncyI7czoxOiIwIjtzOjQ6ImhlbHAi
O3M6MDoiIjtzOjM6Im1kNSI7czozMjoiY2M5ZDE3YTc2YWFmYzhkMjA0M2RjZGZiMzhkNmVl
OTkiO30=
Offline
Re: importing files into textpattern from a url within a plugin
Aside from the fact it doesn’t play nicely with smd_thumbnail for some reason (my plugin probably needs tweaking to hook into yours properly), this is ace.
I’d love to be able to offer drag/drop a URL natively on the Browse… control (like you did in com_article_image when dropping URLs on the Body field), but I was thwarted when I tried it on the Plugins panel and had to use the textarea to house the URL instead – as you’ve done here with the input box. Guess the web controls aren’t set up to handle URLs properly yet without assistance.
One tweak you might like to make for those of us with tempdir paths that don’t end with a trailing slash:
rtrim($tempdir, DS).DS.md5($name).'.tmp';
Just a thought to avoid ugly ass errors from appearing.
I wonder if we could consider something along these lines for a core addition if we could somehow make it nice and unobtrusive as part of the regular upload widget in 4.9. Same for Files. Maybe a Browse button that you can toggle for an Upload URL control or something and it remembers which one you used for next time? That’d be sweeeeet.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: importing files into textpattern from a url within a plugin
I have updated the code to enable multiple imports (one URL per line).
Bloke wrote #328973:
it doesn’t play nicely with smd_thumbnail for some reason
Please re-try with the latest 4.8.5. A possible reason is that thumbnail class was not included in this stage… because it was include later in core. Now it is include_once
everywhere.
Guess the web controls aren’t set up to handle URLs properly yet without assistance.
<input type="file" />
is a tough one, we’d have to fake it.
rtrim($tempdir, DS).DS.md5($name).'.tmp';...
Done, thanks!
I wonder if we could consider something along these lines for a core addition if we could somehow make it nice and unobtrusive as part of the regular upload widget in 4.9. Same for Files. Maybe a Browse button that you can toggle for an Upload URL control or something and it remembers which one you used for next time? That’d be sweeeeet.
There were some vague plans to switch to Uppy or other advanced file uploader in 4.9, we will see then.
Offline
Re: importing files into textpattern from a url within a plugin
etc wrote #328974:
I have updated the code to enable multiple imports (one URL per line).
Niiice!
Please re-try with the latest 4.8.5. Now [class.thumb] is
include_once
everywhere.
Yep, that fixed it, thank you.
There were some vague plans to switch to Uppy or other advanced file uploader in 4.9…
Ah yes. Let’s see what’s possible.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: importing files into textpattern from a url within a plugin
👍👍👍 You two are just a goldmine of goodies! 👍👍👍
TXP Builders – finely-crafted code, design and txp
Offline
#8 2021-02-20 00:30:58
- saccade
- Plugin Author
- From: Neubeuern, Germany
- Registered: 2004-11-05
- Posts: 521
Re: importing files into textpattern from a url within a plugin
wow, great guys! Such a huge response!
Unfortunately I have been busy with other demands, so I now need some sleep, and will check your solutions tomorrow. But it’s a good feeling to have you around.
some notes:
I don’t have direct access to the other (event) database – only an XML-query, which delivers an url to the image.
The XML I import automatically into textpattern, generating articles from every event in the XML, and I would like to have the images natively in textpattern as well, because textpattern will be the permanent place for information. (in the event database all information will be deleted after an event, which of course is not very clever, but cannot be changed).
An because I am not the person who will take care for the data and the persons which will do not have time and skill to transfer the images themselves I would love to get this implemented on an automatic base.
again, thank you …. maybe it’s not time to sleep now, but instead look and test ;)
Offline
#9 2021-02-20 01:12:40
- saccade
- Plugin Author
- From: Neubeuern, Germany
- Registered: 2004-11-05
- Posts: 521
Re: importing files into textpattern from a url within a plugin
this is great!
the additional definition field is a great improvement imho!
tomorrow I’ll check how to integrate it in my import code.
I imagine some function which will take the url of the image, an attribute to create a thumbnail (with my standard dimensions) or a second url which points to an existing thumbnail as well (in fact I have both in the XML, image and thumbnail-urls), and then import those things into txp.
just wandering thoughts: wouldn’t it be named comprehensible: e.g. “fetch”?
because it fetches what I appoint? (ok, I have my doggies in mind :) )
fetch_image
and
fetch_file?
Thanks again!
Offline
Re: importing files into textpattern from a url within a plugin
saccade wrote #328982:
tomorrow I’ll check how to integrate it in my import code.
Not sure for your case, but this has given me an idea of a tiny public-side posting plugin: etc_post. For example, if both etc_post
and etc_import
are installed, a logged-in user could import an image by visiting a page containing the following tag:
<txp:etc_post event="image" step="image_list">
import="https://dev-demo.textpattern.co/dev/images/10.jpg"
</txp:etc_post>
You should wrap it in some conditional tag to avoid accidental posting. You can also post articles and other admin stuff from the public side, yield if interested.
Offline