2

I have a S.pipe for upload and manipulate incoming request file

S.pipe([
    getRequestFile,
    S.chain(saveTemporary),
    S.chain(checkIfIsImage),
    S.chain(addWatermarkToImage),   // only execute on image
    S.chain(copyImageToPublicPath), // only execute on image
    S.chain(copyFileToPath),
    S.chain(formatResponse),
]);

There are 2 specific steps addWatermarkToImage and copyImageToPublicPath that should only execute for image files.

I know I can return Left from checkIfIsImage if the file is not an image but by doing that copyFileToPath and formatResponse are also ignored.

I want to ignore just addWatermarkToImage and copyImageToPublicPath if the file is not image

How can I do that?

mohsen saremi
  • 685
  • 8
  • 22
  • Just determine the condition outside the pipeline and provide a distinct pipeline for each branch. –  Nov 04 '19 at 15:25

1 Answers1

2

The general approach is to use a function such as x => p (x) ? f (x) : g (x). g could be a trivial function such as S.I or S.Right.

In your case the code would resemble the following:

S.pipe ([
  getRequestFile,
  S.chain (saveTemporary),
  S.chain (file => file.type === 'image' ?
                   S.chain (copyImageToPublicPath)
                           (addWatermarkToImage (file)) :
                   S.Right (file)),
  S.chain (copyFileToPath),
  S.chain (formatResponse),
])

Note that it's necessary to return S.Right (file) rather than file in the non-image case.

davidchambers
  • 23,918
  • 16
  • 76
  • 105
  • Thank your for helping me. I have another question here: https://stackoverflow.com/questions/58697289/execute-fluture-task-in-middle-of-sanctuary-pipe . I think you can help me with that too. Please read my question and help me if you can. – mohsen saremi Nov 04 '19 at 16:19