- 
                Notifications
    
You must be signed in to change notification settings  - Fork 5.5k
 
New Components - shortpixel #15516
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
          
     Merged
      
      
    
  
     Merged
                    New Components - shortpixel #15516
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            118 changes: 118 additions & 0 deletions
          
          118 
        
  components/shortpixel/actions/optimize-image/optimize-image.mjs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import shortpixel from "../../shortpixel.app.mjs"; | ||
| import { ConfigurationError } from "@pipedream/platform"; | ||
| import fs from "fs"; | ||
| 
     | 
||
| export default { | ||
| key: "shortpixel-optimize-image", | ||
| name: "Optimize Image", | ||
| description: "Optimize and/or adjust an image using ShortPixel. [See the documentation](https://shortpixel.com/knowledge-base/article/shortpixel-adaptive-images-api-parameters/)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| shortpixel, | ||
| url: { | ||
| type: "string", | ||
| label: "URL", | ||
| description: "The URL of the image to optimize", | ||
| }, | ||
| width: { | ||
| type: "integer", | ||
| label: "Width", | ||
| description: "The width in pixels of the new image", | ||
| optional: true, | ||
| }, | ||
| height: { | ||
| type: "integer", | ||
| label: "Height", | ||
| description: "The height in pixels of the new image", | ||
| optional: true, | ||
| }, | ||
| cropStyle: { | ||
| type: "string", | ||
| label: "Crop Style", | ||
| description: "The crop style, useful when both width and height are specified", | ||
| options: [ | ||
| "top", | ||
| "right", | ||
| "bottom", | ||
| "left", | ||
| "center", | ||
| ], | ||
| optional: true, | ||
| }, | ||
| quality: { | ||
| type: "string", | ||
| label: "Quality", | ||
| description: "The quality setting of the new image", | ||
| options: [ | ||
| "lqip", | ||
| "lossless", | ||
| "glossy", | ||
| "lossy", | ||
| ], | ||
| optional: true, | ||
| }, | ||
| filename: { | ||
| type: "string", | ||
| label: "Filename", | ||
| description: "Optionally, enter a filename that will be used to save the image in /tmp", | ||
| optional: true, | ||
| }, | ||
| }, | ||
| methods: { | ||
| buildParams() { | ||
| const paramArray = [ | ||
| "ret_wait ", | ||
| ]; | ||
| if (this.width) { | ||
| paramArray.push(`w_${this.width}`); | ||
| } | ||
| if (this.height) { | ||
| paramArray.push(`h_${this.height}`); | ||
| } | ||
| if (this.cropStyle) { | ||
| paramArray.push(`c_${this.cropStyle}`); | ||
| } | ||
| if (this.quality) { | ||
| paramArray.push(`q_${this.quality}`); | ||
| } | ||
| return paramArray.join(","); | ||
| }, | ||
| downloadFileToTmp(file, filePath) { | ||
| const rawcontent = file.toString("base64"); | ||
| const buffer = Buffer.from(rawcontent, "base64"); | ||
| fs.writeFileSync(filePath, buffer); | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| if (!this.width && !this.height && !this.cropStyle && !this.quality) { | ||
| throw new ConfigurationError("Must enter at least one of `width`, `height`, `cropStyle`, or `quality`"); | ||
| } | ||
| 
     | 
||
| const params = this.buildParams(); | ||
| 
     | 
||
| let response = { | ||
| url: `${this.shortpixel._baseUrl()}/client/${params}/${this.url}`, | ||
| }; | ||
| 
     | 
||
| try { | ||
| const image = await this.shortpixel.optimizeImage({ | ||
| $, | ||
| params, | ||
| url: this.url, | ||
| responseType: "arraybuffer", | ||
| }); | ||
| if (this.filename) { | ||
| const filePath = this.filename.includes("tmp/") | ||
| ? this.filename | ||
| : `/tmp/${this.filename}`; | ||
| this.downloadFileToTmp(image, filePath); | ||
| response.filePath = filePath; | ||
| } | ||
| } catch { | ||
| throw new Error(`Unable to process image at URL: ${this.url}`); | ||
| } | ||
                
      
                  michelle0927 marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| return response; | ||
| }, | ||
| }; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,11 +1,35 @@ | ||
| import { axios } from "@pipedream/platform"; | ||
| 
     | 
||
| export default { | ||
| type: "app", | ||
| app: "shortpixel", | ||
| propDefinitions: {}, | ||
| methods: { | ||
| // this.$auth contains connected account data | ||
| authKeys() { | ||
| console.log(Object.keys(this.$auth)); | ||
| _baseUrl() { | ||
| return "https://cdn.shortpixel.ai"; | ||
| }, | ||
| _makeRequest({ | ||
| $ = this, | ||
| path, | ||
| params, | ||
| ...opts | ||
| }) { | ||
| return axios($, { | ||
| url: `${this._baseUrl()}${path}`, | ||
| params: { | ||
| ...params, | ||
| key: this.$auth.api_key, | ||
| }, | ||
| ...opts, | ||
| }); | ||
| }, | ||
                
      
                  michelle0927 marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| optimizeImage({ | ||
| params, url, ...opts | ||
| }) { | ||
| return this._makeRequest({ | ||
| path: `/client/${params}/${url}`, | ||
| ...opts, | ||
| }); | ||
| }, | ||
                
      
                  michelle0927 marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| }, | ||
| }; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.