Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Pedro Mendes 33 posts 187 karma points
    Jul 20, 2022 @ 14:20
    Pedro Mendes
    0

    Unable to create new media via NodeJS client

    Hey there!

    I'm trying to create a new media using the nodeJS client.

    This is the code I have:

    `const form = new FormData();

        let mediaContent = JSON.stringify({
            mediaTypeAlias: "Image",
            parentId: parentId,
            name: fileName,
            umbracoFile: { src: path.basename(fileUrl) }
        });
    
        form.append("content", mediaContent, {contentType: 'application/json'});
    
        const image = await axios.get(fileUrl, { responseType: 'arraybuffer'});        
    
        form.append('umbracoFile', image.data.toString('binary'), {contentType: 'image/png'});
        let r = await client.management.media.create(form);`
    

    The media itself is created, but it doesn't have a thumbnail and whenever I try to open in, I get the following error:

    enter image description here

    Has anyone faced this issue? I'm really lost here and don't know what I'm doing wrong :\

    Thanks

  • Lindow 106 posts 1000 karma points admin hq
    Jul 20, 2022 @ 14:40
    Lindow
    1

    Hi there

    Which file type are you uploading?

    As for the blank error, check the log viewer in the settings section for the proper stack trace :)

  • Pedro Mendes 33 posts 187 karma points
    Jul 20, 2022 @ 15:26
    Pedro Mendes
    0

    Hey there Lindow, thanks for the fast reply!

    The file type is PNG.

    The error in the log viewer does not give much information on what to do... I'll paste it here (pardon the length)

    System.ArgumentException: Path must start with a slash. Parameter name: path at Umbraco.Core.UriExtensions.Rewrite(Uri uri, String path) at Umbraco.Web.Routing.DefaultMediaUrlProvider.AssembleUrl(String path, Uri current, UrlMode mode) at Umbraco.Web.Routing.DefaultMediaUrlProvider.GetMediaUrl(UmbracoContext umbracoContext, IPublishedContent content, String propertyAlias, UrlMode mode, String culture, Uri current) at Umbraco.Cloud.StorageProviders.AzureBlob.CdnMediaUrlProvider.GetMediaUrl(UmbracoContext umbracoContext, IPublishedContent content, String propertyAlias, UrlMode mode, String culture, Uri current) at Umbraco.Web.Routing.UrlProvider.<>cDisplayClass21_0.0(IMediaUrlProvider provider) at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate) at Umbraco.Web.Routing.UrlProvider.GetMediaUrl(IPublishedContent content, UrlMode mode, String culture, String propertyAlias, Uri current) at Umbraco.Cloud.Headless.Backoffice.Compose.BackofficeComponent.EditorModelEventManager_OnSendingMediaModel(HttpActionExecutedContext sender, EditorModelEventArgs1 e) at Umbraco.Web.Editors.EditorModelEventManager.EmitEvent(HttpActionExecutedContext sender, EditorModelEventArgs e) at Umbraco.Web.WebApi.Filters.OutgoingEditorModelEventAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext) at System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.

  • Lindow 106 posts 1000 karma points admin hq
    Jul 20, 2022 @ 15:47
    Lindow
    0

    Okay, what exactly is the value of path.basename(fileUrl)? because it needs to be a working path to a file and there seems to be an issue with yours.

    I have some HTTP calls saved in my Insomnia but I can't get the software to open for some reason, so here's the next best thing - https://github.com/umbraco/Umbraco.Cloud.Issues/issues/539#issue-1126477238

    It's an issue we had a while back which is now resolved but I've pasted some cURLs for both image and file media types on the thread .

    You're using the Image media type which is also what you should have and given the error you get, it might be worth checking how the file path looks like :)

  • Pedro Mendes 33 posts 187 karma points
    Jul 20, 2022 @ 15:59
    Pedro Mendes
    0

    Thanks again for the fast reply :)

    The path.basename(fileUrl) returns the name of the file (i.e. "image.png") as per Official Umbraco Headless example.

    The only difference I have is that the dataumbracoFile appendend to the FormData is a binary string while in the example is a fs.ReadStream.

    Maybe I have to save the image locally and upload it using node's file system module..

  • Lindow 106 posts 1000 karma points admin hq
    Jul 20, 2022 @ 16:10
    Lindow
    1

    Oh, yeah, I get that part :)

    I meant the local file path like C:\Users\Lindow\Desktop\capybara.png

    Maybe the path isn't correct and the file cannot be accessed through it.

    You can eventually try to create and upload the file using an HTTP client like Postman to see if that works for you.

    If that's not the case then I'll check with one of the guys who made this SDK, as I'm not familiar with them but only with the Heartcore functionality.

  • Pedro Mendes 33 posts 187 karma points
    Jul 20, 2022 @ 16:32
    Pedro Mendes
    1

    I'll try to save the image locally and then upload it exactly as the official example :)

    If, by any change, you could check with any dev from the SDK it would be great! It is missing some documentation as it is :\

  • Pedro Mendes 33 posts 187 karma points
    Jul 20, 2022 @ 16:49
    Pedro Mendes
    100

    I've just tested it by downloading the image and then using the File System module's createReadStream() and it was uploaded!

    It is something with the way I was uploading it.. but I don't know what it is :\

  • Lindow 106 posts 1000 karma points admin hq
    Jul 21, 2022 @ 07:32
    Lindow
    0

    Hi again

    Could you create a new issue on the NodeJS tracker https://github.com/umbraco/Umbraco.Headless.Client.NodeJs/issues/new and explain what documentation you're missing?

    Also, please mark this thread as resolved if you've figured out your issue. You can do that by clicking the checkmark under your post :)

  • Pedro Mendes 33 posts 187 karma points
    Jul 21, 2022 @ 10:07
    Pedro Mendes
    1

    Will do! Thanks Lindow

  • Lindow 106 posts 1000 karma points admin hq
    Jul 21, 2022 @ 10:39
Please Sign in or register to post replies

Write your reply to:

Draft