Skip to content

Commit

Permalink
Update ilovepdf extension
Browse files Browse the repository at this point in the history
- chore: make title titled cased
- Merge branch \'contributions/merge-1715547025023987000\' into ilovepdf
- Pull contributions
- Merge branch \'contributions/merge-1715541634996198000\' into ilovepdf
- Pull contributions
- feat: add compress PDF command in iLovePDF extension
- [ADB] Add Set animation scale and Restart ADB commands (raycast#12121)
- Support file names with special chars (raycast#12205)
- Update CODEOWNERs
- Add ilovepdf extension (raycast#12085)
- Update CODEOWNERs
- [Badges][Brands Icons] Publish Badges extension & update Brand Icons (raycast#12084)
- Update open-graph extension (raycast#12071)
- Update CODEOWNERs
- Update tennis-standings extension (raycast#12063)
- Update CODEOWNERs
- Add datawrapper extension (raycast#12057)
- [Logitech Litra] Add support for \`litra-rs\` CLI v1.x (raycast#12054)
- Update CODEOWNERs
- Add browser-ai extension (raycast#12049)
- Update workouts extension (raycast#12044)
- Update CODEOWNERs
- Add youtube-shorts-to-normal-video-page extension (raycast#12042)
- [Perplexity API] updated models and minor fixes (raycast#12186)
- Update key value extension to get the json from remote URL (raycast#12187)
- [spotify-control] feat: added command to mute volume in spotify (raycast#12199)
- Update CODEOWNERs
- [Toothpick] Add commands for Toggling devices (raycast#12157)
- [Amazon AWS] Add ECR Repositories command (raycast#12191)
- Update CHANGELOG.md
- Update CODEOWNERs
- Add bookstack extension (raycast#12036)
- Update CODEOWNERs
- Update CODEOWNERs
- Update zed-recent-projects extension (raycast#12137)
- Add composerize extension (raycast#12035)
- Update CODEOWNERs
- Update sonarr extension (raycast#12032)
- Update CODEOWNERs
- [Shell] Fixes iTerm Double Window Bug (raycast#12028)
- Update doutu extension (raycast#12027)
- remove deprecated commands for scrcpy (raycast#12026)
- Update CODEOWNERs
- remove Davidwalser from contributor list (raycast#12144)
- Update radicle extension (raycast#12146)
- Update hyrule-compendium-search extension (raycast#12109)
- Update omnivore extension (raycast#12083)
- Update CODEOWNERs
- Update chatgpt extension (raycast#12081)
- Feat add recap view for pomodoro extention  (raycast#11944)
- Update CODEOWNERs
- Add syntax-fm extension (raycast#12021)
- Fix Raindrop typo in placeholder (raycast#12127)
- Update CODEOWNERs
- Add rewardful extension (raycast#12055)
- Update jsr extension (raycast#12019)
- Update shortcuts-search extension (raycast#12006)
- Update exif extension (raycast#12089)
- update(yafw): Don\'t shadow errors and fix ffmpeg path logic (raycast#12099)
- [update] Summarize YouTube Video - broken fetch (raycast#12108)
- Fix r-pkg-search extension (raycast#12111)
- farcaster extension: add support for nook (raycast#12123)
- Update toothpick extension (raycast#12125)
- Update CODEOWNERs
- Update mac-app-store-search extension (raycast#12091)
- Update CODEOWNERs
- Add node-release-notes extension (raycast#10871)
- Update CODEOWNERs
- [Google translate] Add a preference for the default action (raycast#12078)
- Update missing Linear icons (raycast#12116)
- Update CODEOWNERs
- Update jira extension (raycast#11965)
- Update CODEOWNERs
- Add capacities extension (raycast#11935)
- Docs: Update the utils docs
- Update CODEOWNERs
- Add nuget-package-explorer extension (raycast#11993)
- [Groq] minor fixes and updates (raycast#11990)
- Fix typo in README of jira-search-self-hosted (raycast#12096)
- Update outline-page extension (raycast#11955)
- Removed the double style
- Update CODEOWNERs
- Update terraform-doc extension (raycast#11947)
- Update CODEOWNERs
- Update json-format extension (raycast#11938)
- Update contribute-to-an-extension.md
- Update CODEOWNERs
- Update supernotes extension (raycast#11598)
- Update power-management extension (raycast#11917)
- Update CODEOWNERs
- Update wayback-machine extension (raycast#12072)
- Update CODEOWNERs
- Update openai-translator extension (raycast#11579)
- Update CODEOWNERs
- Add mac-app-store-search extension (raycast#11903)
- Update CODEOWNERs
- Add search-private-npm-packages extension (raycast#11916)
- Update CODEOWNERs
- Fixed Change Case overlapping shortcuts (raycast#11997)
- [Year In Progress] (Show|Hide) in Command Subtitle (raycast#11912)
- Add yafw extension (raycast#11894)
- [Copilot Workspace] Allow all valid github repo characters (raycast#12079)
- Update CODEOWNERs
- transfer to org (raycast#12077)
- Update copilot-workspace extension (raycast#12074)
- Apple Reminders: Improve regex
- Update CODEOWNERs
- Add copilot-workspace extension (raycast#11961)
- Update CODEOWNERs
- Update toothpick extension (raycast#11939)
- Update snap-jot extension (raycast#12041)
- Update hyrule-compendium-search extension (raycast#12047)
- CI: add batch processing of images due to ImageOptim CLI issue JamieMason/ImageOptim-CLI#205
- Linear: Add pagination to issues search (raycast#11843)
- [BetterTouchTool] Error: Command was killed with null... (raycast#12066)
- Update Linear and Slack icons (raycast#12022)
- Update bintools extension (raycast#12069)
  • Loading branch information
MohamedK1 committed May 12, 2024
1 parent 7ca3463 commit 0ec051b
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
2 changes: 2 additions & 0 deletions extensions/ilovepdf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# iLovePDF Changelog

## [Add Compress PDF] - 2024-05-06

## [Initial Version] - 2024-04-27
9 changes: 8 additions & 1 deletion extensions/ilovepdf/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://www.raycast.com/schemas/extension.json",
"name": "ilovepdf",
"title": "iLovePDF",
"title": "ILovePDF",
"description": "Set of commands to interact with iLovePDF",
"icon": "command-icon.png",
"author": "mohamedk1",
Expand All @@ -16,6 +16,13 @@
"subtitle": "iLovePDF",
"description": "Converts an image to pdf using iLovePDF API.",
"mode": "view"
},
{
"name": "compress-pdf",
"title": "Compress PDF",
"subtitle": "iLovePDF",
"description": "Compresses pdf using iLovePDF API.",
"mode": "view"
}
],
"preferences": [
Expand Down
156 changes: 156 additions & 0 deletions extensions/ilovepdf/src/compress-pdf.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import {
Form,
ActionPanel,
Action,
showToast,
getPreferenceValues,
Toast,
closeMainWindow,
open,
openExtensionPreferences,
} from "@raycast/api";
import ILovePDFApi from "@ilovepdf/ilovepdf-nodejs";
import CompressTask from "@ilovepdf/ilovepdf-js-core/tasks/CompressTask";
import ILovePDFFile from "@ilovepdf/ilovepdf-nodejs/ILovePDFFile";
import { useState } from "react";
import fs from "fs";
import path from "path";

type Values = {
files: string[];
compression_level: "low" | "recommended" | "extreme";
};

type Status = "init" | "success" | "failure";

const { APIPublicKey: publicKey, APISecretKey: secretKey, OpenNow: openNow } = getPreferenceValues<Preferences>();

function getDestinationFile(files: string[]): string {
if (!files.length) {
return "";
}
const file = files[0];
const fileExtension = path.extname(file);
const fileName = path.basename(file, fileExtension);
const directory = path.dirname(file);
if (files.length == 1) {
return path.join(directory, `${fileName}_compressed.pdf`);
}
return path.join(directory, `compressed_pdfs.zip`);
}

function getSavedPercentage(originalFile: string, compressedFile: string) {
const originalSize = fs.statSync(originalFile).size;
const compressedSize = fs.statSync(compressedFile).size;
return 100 - Math.floor((compressedSize / originalSize) * 100);
}

export default function Command() {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [status, setStatus] = useState<Status>("init");
const [destinationFilePath, setDestinationFilePath] = useState<string>("");

async function handleSubmit(values: Values) {
setIsLoading(true);
if (!values.files.length) {
await showToast(Toast.Style.Failure, "You must select at least a single pdf file", "Please select a file");
setStatus("failure");
setIsLoading(false);
return;
}

const toast = await showToast(Toast.Style.Animated, "Processing", "Compressing PDF...");

let destinationFile = "";
const instance = new ILovePDFApi(publicKey, secretKey);
const task = instance.newTask("compress") as CompressTask;
const addedFilesPromises = [];
try {
await task.start();
for (const file of values.files) {
const fileExtension = path.extname(file);
if (fileExtension != ".pdf") {
toast.style = Toast.Style.Failure;
toast.title = "failure";
toast.message = "You must select a PDF file.";
setStatus("failure");
setIsLoading(false);
console.log(`file is not a PDF received extension is ${fileExtension}`);
return;
}
const iLovePdfFile = new ILovePDFFile(file);
addedFilesPromises.push(task.addFile(iLovePdfFile));
}
await Promise.all(addedFilesPromises);
destinationFile = getDestinationFile(values.files);
setDestinationFilePath(destinationFile);
await task.process({ compression_level: values.compression_level });
const data = await task.download();
fs.writeFileSync(destinationFile, data);

toast.style = Toast.Style.Success;
toast.title = "success";
toast.message =
"Compressed successfully." +
(values.files.length == 1
? ` Your PDF is ${getSavedPercentage(values.files[0], destinationFile)}% smaller`
: "");
setStatus("success");
setIsLoading(false);
} catch (error) {
toast.style = Toast.Style.Failure;
toast.title = "failure";
toast.message = "Error happened during compressing the file.";
setStatus("failure");
setIsLoading(false);
console.log(error);
return;
}

if (openNow) {
await closeMainWindow();
open(destinationFile);
} else {
toast.primaryAction = {
title: "Open File",
onAction: () => {
open(destinationFile);
},
};
}
}

return (
<Form
enableDrafts
actions={
<ActionPanel>
<Action.SubmitForm title="Compress PDF" onSubmit={handleSubmit} />
{status == "success" ? <Action.ShowInFinder title="Show in Finder" path={destinationFilePath} /> : null}
{status == "success" ? (
<Action.CopyToClipboard title="Copy Path to Clipboard" content={destinationFilePath} />
) : null}
{status == "success" ? (
<Action
title="Open File"
onAction={() => {
open(destinationFilePath);
}}
/>
) : null}
{status == "failure" ? (
<Action title="Open Extension Preferences" onAction={openExtensionPreferences} />
) : null}
</ActionPanel>
}
isLoading={isLoading}
>
<Form.FilePicker id="files" title="Choose PDF files" allowMultipleSelection={true} />
<Form.Dropdown id="compression_level" title="Compression Level" defaultValue="recommended">
<Form.Dropdown.Item value="recommended" title="Recommended" />
<Form.Dropdown.Item value="extreme" title="Extreme" />
<Form.Dropdown.Item value="low" title="Low" />
</Form.Dropdown>
</Form>
);
}

0 comments on commit 0ec051b

Please sign in to comment.