Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Can't get static file/image hosting to work

Vote:
 

On my dev machine, I have a headless version of optimizely running.

In the cms when uploading an image (vf.jpg) it creates a apps\optimizely\App_Data\blobs\4f4864ab0dbd41aa8065dca34377ce24\47549b475bbe41bfb62a31b70a8e25a4.jpg file on the file system.

The image thumbnail does show a thumbnail version of the image. The image preview does not. It gives me 2 404's

http://localhost:8000/EPiServer/CMS/Content/globalassets/vf.jpg,,48?epieditmode=False

And

http://localhost:8000/EPiServer/CMS/Content/globalassets/vf.jpg,,48?epieditmode=False and http://localhost:8000/EPiServer/CMS/ImageEditor/GetImage?epieditmode=true&zoom=100&quality=100&img=http://localhost:8000/EPiServer/CMS/Content/globalassets/vf.jpg,,47?epieditmode=False&commands=

Also calling http://localhost:8000/EPiServer/CMS/Content/globalassets/vf.jpg from the browser gives a 404.

In the startup.cs I use:

app.UseStaticFiles(); // Existing static files middleware

    // Add the new static files middleware to serve files from the blob storage folder
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new BlobFileProvider(
            Path.Combine(env.ContentRootPath, "App_Data", "blobs")),
        RequestPath = "/EPiServer/CMS/Content/globalassets"
    });

and blobfileprovider looking like:

using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using System;
using System.IO;

public class BlobFileProvider : IFileProvider
{
  private readonly string _root;

  public BlobFileProvider(string root)
  {
    _root = root;
  }

  public IDirectoryContents GetDirectoryContents(string subpath)
  {
    return new PhysicalFileProvider(_root).GetDirectoryContents(subpath);
  }

  public IFileInfo GetFileInfo(string subpath)
  {
    string[] pathSegments = subpath.Split(new[] { "/globalassets/" }, StringSplitOptions.RemoveEmptyEntries);

    if (pathSegments.Length != 1)
    {
      return new NotFoundFileInfo(subpath);
    }

    string newPath = Path.Combine(_root, pathSegments[0].Replace('/', '\\'));
    return new PhysicalFileProvider(Path.GetDirectoryName(newPath)).GetFileInfo(Path.GetFileName(newPath));
  }

  public IChangeToken Watch(string filter)
  {
    return new PhysicalFileProvider(_root).Watch(filter);
  }

  IChangeToken IFileProvider.Watch(string filter)
  {
    throw new NotImplementedException();
  }
}

Im new to optimizely (headless). Any idea what im doing wrong?

#301464
May 10, 2023 11:25
Vote:
 

May I ask why you have your own file provider? CMS already ships with a blob handler that serves files uploaded to the CMS. A static file provider serves files from the local filesystem, files uploaded in CMS are not necessary stored on the local filesystem. Having a static file provider won't support horizontal scaling of the application either, unless you store them on a common file share, or sync them between the instances. If you deploy to e.g. Azure Web apps, local files are not persisted, they are removed when the instances are removed due to the application scaling back.

The URLs that 404's, are using routing, and are routed to the built-in blob handler. With your static file handler, the URLs to the files are completely different, they should match the exact structure as the local filesystem.

#301465
Edited, May 10, 2023 13:13
Vote:
 

i couldnt get the out of box to work. No matter what i try.

#301466
May 10, 2023 13:17
Vote:
 

How does your appsettings.config (or appsettings.developmen.json) look like?

#301467
May 10, 2023 13:22
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.