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

Usage in factories? #14

Open
evoactivity opened this issue Sep 7, 2022 · 6 comments
Open

Usage in factories? #14

evoactivity opened this issue Sep 7, 2022 · 6 comments

Comments

@evoactivity
Copy link

evoactivity commented Sep 7, 2022

I have a factory like so

import Factory from '@ioc:Adonis/Lucid/Factory';
import ServiceProviderProfile from 'App/Models/ServiceProviderProfile';
import { ResponsiveAttachment } from '@ioc:Adonis/Addons/ResponsiveAttachment';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

export default Factory.define(ServiceProviderProfile, async ({ faker }) => {

  const avatarImage = await ResponsiveAttachment.fromBuffer(
    await readFile(join(__dirname, '../../tests/resources/default-avatar.png'))
  );

  await avatarImage.setOptions({
    forceFormat: 'webp',
    keepOriginal: true,
    folder: 'avatars/service-provider',
    breakpoints: {
      xlarge: 1200,
      large: 800,
      medium: 300,
      small: 100
    }
  });
  await avatarImage.save();

  return {
    name: faker.name.findName(),
    avatar: avatarImage, // this ends up as null
    about: faker.random.words(100),
    specialties: faker.random.words(5).split(' ').join(', ')
  };
}).build();

The images are processed and saved to disk. avatarImage is the correct object before the return. Is there something else I need to do to make this work in a factory?

@evoactivity
Copy link
Author

So I've done some digging and found that if in my model decorator I have preComputeUrls set to false it works as expected with this factory

import { ResponsiveAttachment } from '@ioc:Adonis/Addons/ResponsiveAttachment';
import Factory from '@ioc:Adonis/Lucid/Factory';
import ServiceProviderProfile from 'App/Models/ServiceProviderProfile';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

export default Factory.define(ServiceProviderProfile, async ({ faker }) => {
  const avatarImage = await ResponsiveAttachment.fromBuffer(
    await readFile(join(__dirname, '../../tests/resources/default-avatar.png'))
  );

  return {
    name: faker.name.fullName(),
    avatar: avatarImage,
    type: faker.helpers.arrayElement(['mental', 'physical']) as 'mental' | 'physical',
    about: faker.random.words(30),
    specialties: faker.random.words(5).split(' ').join(', ')
  };
}).build();

Drive.getUrl/getSignedUrl does not seem to work outside of a response handler.

@ndianabasi
Copy link
Owner

Please confirm if this still persist in v1.5.0. Thank you.

@evoactivity
Copy link
Author

Seems to be working for me now when preComputeUrls is set to true! Thank you!!

If you don't mind me asking, how was it fixed?

@ndianabasi
Copy link
Owner

ndianabasi commented Jan 12, 2023

I can't say what fixed it as I haven't used the add-on in a factory context yet 😄. But I definitely know that a lot of improvements where made from v1.5.0.

Glad that it is working for you as expected.

@evoactivity
Copy link
Author

I may have closed too early. Seems fine when running in a test context, but in a seeder context it fails.

Adonis Responsive Attachment error: {
	"stack": "RouterException: E_CANNOT_FIND_ROUTE: Cannot find route for \"drive.local.serve\" identifier
	    at Function.cannotLookupRoute (/Users/anon/development/server/node_modules/@adonisjs/http-server/build/src/Exceptions/RouterException.js:63:23)
	    at Routes.findOrFail (/Users/anon/development/server/node_modules/@adonisjs/http-server/build/src/Router/LookupStore.js:39:53)
	    at UrlBuilder.makeSigned (/Users/anon/development/server/node_modules/@adonisjs/http-server/build/src/Router/LookupStore.js:180:39)
	    at Router.makeSignedUrl (/Users/anon/development/server/node_modules/@adonisjs/http-server/build/src/Router/index.js:330:24)
	    at LocalDriver.getSignedUrl (/Users/anon/development/server/node_modules/@adonisjs/drive/build/src/Drivers/Local.js:142:28)
	    at ResponsiveAttachment.computeUrls (/Users/anon/development/server/node_modules/adonis-responsive-attachment/build/src/Attachment/index.js:418:42)
	    at ResponsiveAttachment.save (/Users/anon/development/server/node_modules/adonis-responsive-attachment/build/src/Attachment/index.js:320:9)
	    at persistAttachment (/Users/anon/development/server/node_modules/adonis-responsive-attachment/build/src/Attachment/decorator.js:61:32)
	    at async Promise.all (index 0)
	    at Proxy.saveWithAttachments (/Users/anon/development/server/node_modules/adonis-responsive-attachment/build/src/Attachment/decorator.js:98:5)",
	"message": "E_CANNOT_FIND_ROUTE: Cannot find route for \"drive.local.serve\" identifier",
	"name": "RouterException",
	"status": 500,
	"code": "E_CANNOT_FIND_ROUTE",
	"help": "When making URL for a route. You can use one of the following identifiers\n- The route name. Defined using `Route.as()`\n- The route controller.method name. `PostsController.show`"
}

@evoactivity evoactivity reopened this Jan 12, 2023
@evoactivity
Copy link
Author

This is a short conversation about it on discord
https://discord.com/channels/423256550564691970/1028339813142778017/1028339813142778017

So I think something in adonis itself would need to change to make it work properly in every context.

ndianabasi pushed a commit that referenced this issue Feb 24, 2023
* feat: upgrade to new japa version

* style(test): run prettier
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

2 participants