Best way to upload files with Restangular + Api-platform (Symfony2) - symfony

I use api-platform for the first time and I am looking for the best way to upload files.
My situation :
An entity "Post" with a OneToMany relation "Media" (SonataMedia) on server side, I'm working on the create view (Angular + Restangular).
All fields (except file type fields) are functional (persist OK). Now, for the file type fields, what do I have to do?
Asynchronous upload? in this case how to link files upload with my entity (which is not persisted yet)
upload files on form entity submit?
In every case, what it needs to send to server? How Api-platform manages it?

There is no builtin file upload support as time of writing in API Platform. You need to write your own code to handle uploads.
However, some work is some work being done to natively add this feature and you can already use it.
Basically, the idea is:
Encode the file to upload as data: URI client-side (using JavaScript)
Send this data URI (basically a base64 string) as a typical string entity property using Restangular (or any other HTTP client)
Decode the data: URI server-side to a regular file using the Symfony Serializer
Store this regular file on the server: it's up to you to store it on the filesystem or in more advanced backends such as Mongo GridFS or Amazon S3
Client-side
To get a data: URI client-side from a selected file, you can rely on the readAsDataURL of the FileReader JavaScript API: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL.
Send the resulting string as usual using Restangular.
Server-side
The API Platform normalization system is built on top of the Symfony Serializer. I've contributed a data: URI normalizer and denormalizer in Symfony: https://github.com/symfony/symfony/pull/16164
It is not merged yet but you can copy it in your own project during the meantime.
Register the normalizer:
services:
data_uri_normalizer:
class: 'AppBundle\Serializer\DataUriNormalizer'
tags:
- { name: serializer.normalizer }
Then create (and register) a new normalizer decorating the ItemNormalizer of API Platform to transform your property containing the file encoded as a data: URI to a standard \SplFileObject using the DataUriNormalizer::denormalize() method.
In a future version of API Platform (planned for the v2.1 with Symfony 3.1) all this logic will be automatically available and registered.

Related

How can i request a new api that was added in publisher-api.yaml

I add a new api in the file below:
carbon-apimgt\components\apimgt\org.wso2.carbon.apimgt.rest.api.publisher.v1\src\main\resources\publisher-api.yaml
Maven build success and generate some new files which are definded in the publisher-api.yaml.
I use postman to invoke this api but get a null response , why? How can i reuqest it?
When you update the publisher-api.yaml with a new resource, you have to build two components.
carbon-apimgt/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1
carbon-apimgt/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common
This will generate the required DTOs and interfaces for the implementation of the new resource.
Then you have to implement the logic behind the new resource. carbon-apimgt/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl will now have a new java file and you can add the logic.

Dynamic URLs in Typescript

I have a web app built using asp.net core, vue.js and typescript. My Typescript code needs to fetch data from an API as follows:
var myFetch = fetch(this.apiurl + '/apipath', {
cache: 'no-cache',
method: 'GET'
})
But I need the URL for the API (this.apiurl) to be configurable instead of hard-coding. The URL will be different for each deployment, and can be on different domains i.e. myapp.clientdomain.com, myapp.otherdomain.com.
Can anyone provide an example of providing a URL dynamically to Typescript so it uses my configured URL instead of a hard-coded one? I have looked at:
MVC providing a JavaScript response (seems overly complex for what I need - just one variable to be set dynamically...)
Creating an API within the .net core web app that returns URLs via a fetch, then calls the actual APIs (URL has to fetch-ed at run-time before subsequent calls to get data, resulting in longer load times)
Building/packaging with a separate URL Typescript file depending on the client (not configurable without a recompile)
Does anyone have suggestions?

Access to raw query parameters in ASP.NET Core

I want to access to raw query parameters in ASP.NET Core 2.0
Currently HttpRequest.Query provides access to strings decoded from url encoded encoding, but it decodes it incorrectly.
How can I access to raw parameters instead of parsing full query string by hands?
Ok, it seems I overcome this issue.
So, how I did it?
I used QueryFeature class from asp.net core source and replaced call to QueryHelpers.ParseNullableQuery (you can find it here) with slightly changed implementation of this helper without call to Uri.UnescapeDataString to parameter value.
Then created middleware class that sets my custom QueryFeature for HttpContext:
context.Features.Set<IQueryFeature>(new MyQueryFeature(context.Features));
and used it during app configure.
Don't forget that after this all your parameters will stay url encoded if they were.

Corda receive attachments from RESTful api

I am building a project using Corda.
I use RESTful api to invoke flows to create and update a state. But how can I upload a pdf file to node using attachment function in Corda?
Any help is appreiciated
If you are using spring servers then use below logic :
create an api that uses the multipart request and include the below
code in it.
msg = proxy.uploadAttachment(new ByteArrayInputStream(getZippedBytes(file))).toString();//msg returns the unique hashid for this attachment.
//getZippedBytes(file)) returns the byte[]
If you're using the built-in webserver, you need to encode the attachment as multipart/form-data and post it to NODE_WEBSERVER_ADDRESS/upload/attachment.
See an example here: https://github.com/corda/samples/blob/release-V3/blacklist/src/main/resources/blacklistWeb/index.html.

Posting base64 encoded files to an asp.net 1.1 page

We are making an automated patching application and would like to post files on production server through asp.net page (or maybe a web service), since we can only access production server via http. The page should accept files and store them to appropriate location. The path to files will be declared in external XML file.
So, is it possible posting a base64 encoded files within body tag and HOW? maybe even any better approach?
If you plan to use Base64 encoding.
Take a look at
System.Convert.ToBase64String()
System.Convert.FromBase64String()
System.Convert.ToBase64CharArray()
System.Convert.FromBase64CharArray()
See Using XML CDATA nodes to send files via a Web Service
why not create a webservice which accepts an object like:
class postfile
{
public byte[] fileByte;
public string fileName;
}
Then add a web reference in your client app.
.net will serialize the object for you.
You will need to secure this using wse security and might require the service use impersonation to write the file on the server.

Resources