Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yourCustomLogic example #27

Open
headwinds opened this issue Sep 18, 2022 · 0 comments
Open

yourCustomLogic example #27

headwinds opened this issue Sep 18, 2022 · 0 comments

Comments

@headwinds
Copy link

headwinds commented Sep 18, 2022

I'm supposed to supply my own function here...

Can you provide an example of what that what that would look like?

I definitely appreciate this library! It's challenging to find a Typescript version - I'm using NestJS for my server


This is how I'm trying to plug it into NestJS - it seems solid except I'm missing yourCustomLogic

import * as multer from 'multer';
import { MulterAzureStorage, MASNameResolver } from 'multer-azure-blob-storage';

const resolveBlobName: MASNameResolver = (
  req: any,
  file: Express.Multer.File,
): Promise<string> => {
  return new Promise<string>((resolve, reject) => {
    const blobName: string = yourCustomLogic(req, file);
    resolve(blobName);
  });
};

export type MetadataObj = { [k: string]: string };
const resolveMetadata: MASObjectResolver = (
  req: any,
  file: Express.Multer.File,
): Promise<MetadataObj> => {
  return new Promise<MetadataObj>((resolve, reject) => {
    const metadata: MetadataObj = yourCustomLogic(req, file);
    resolve(metadata);
  });
};

const resolveContentSettings: MASObjectResolver = (
  req: any,
  file: Express.Multer.File,
): Promise<MetadataObj> => {
  return new Promise<MetadataObj>((resolve, reject) => {
    const contentSettings: MetadataObj = yourCustomLogic(req, file);
    resolve(contentSettings);
  });
};

const azureStorage: MulterAzureStorage = new MulterAzureStorage({
  connectionString: CONNECTION_STRING,
  accessKey: ACCESS_KEY,
  accountName:  ACCOUNT_NAME,
  containerName: 'documents',
  blobName: resolveBlobName,
  metadata: resolveMetadata,
  contentSettings: resolveContentSettings,
  containerAccessLevel: 'blob',
  urlExpirationTime: 60,
});

const upload: multer.Instance = multer({
  storage: azureStorage,
});

@Injectable()
export class PhotoUploadService {
  async fileupload(@Req() req, @Res() res) {
    try {
      upload(req, res, function(error) {
        if (error) {
          console.log('attempting upload ERROR: ', error);
          return res.status(404).json(`Failed to upload image file: ${error}`);
        }

        if (req.files) {
          //console.log("attempting upload ok: ", req.files);
          return res.status(201).json(req.files[0].location);
        } else {
          console.log('attempting upload ERROR! no files!!!');
          return res
            .status(404)
            .json(`Failed to upload image file: the request had no files`);
        }
      });
    } catch (error) {
      console.log(error);
      return res.status(500).json(`Failed to upload image file: ${error}`);
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant