Attempt to make iOS Compatible

This commit is contained in:
Mossy1022 2024-07-02 16:59:12 -04:00
parent 73da7f05d6
commit 0a4e4c94ef
4 changed files with 69 additions and 50 deletions

View file

@ -1,10 +1,11 @@
import { App, TFile, TFolder } from 'obsidian'; import { App, TFile, TFolder, normalizePath } from 'obsidian';
export async function saveFile(app: App, audioBlob: Blob, fileName: string, path: string): Promise<TFile> { export async function saveFile(app: App, audioBlob: Blob, fileName: string, path: string): Promise<TFile> {
try { try {
const filePath = `${path}/${fileName}`; const normalizedPath = normalizePath(path);
const filePath = `${normalizedPath}/${fileName}`;
await directoryExistsAtPath(app, path); await ensureDirectoryExists(app, normalizedPath);
const arrayBuffer = await audioBlob.arrayBuffer(); const arrayBuffer = await audioBlob.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer); const uint8Array = new Uint8Array(arrayBuffer);
@ -20,12 +21,13 @@ export async function saveFile(app: App, audioBlob: Blob, fileName: string, path
} }
} }
async function directoryExistsAtPath(app: App, folderPath: string) { async function ensureDirectoryExists(app: App, folderPath: string) {
const parts = folderPath.split('/'); const parts = folderPath.split('/');
let currentPath = ''; let currentPath = '';
for (const part of parts) { for (const part of parts) {
currentPath = currentPath ? `${currentPath}/${part}` : part; currentPath = currentPath ? `${currentPath}/${part}` : part;
try { try {
const folder = app.vault.getAbstractFileByPath(currentPath); const folder = app.vault.getAbstractFileByPath(currentPath);
if (!folder) { if (!folder) {
@ -36,8 +38,13 @@ async function directoryExistsAtPath(app: App, folderPath: string) {
throw new Error(`${currentPath} is not a folder`); throw new Error(`${currentPath} is not a folder`);
} }
} catch (error) { } catch (error) {
if (error.message.includes('Folder already exists')) {
// Folder already exists, continue to the next part
console.log(`Handled existing folder: ${currentPath}`);
} else {
console.error(`Error ensuring directory exists: ${error.message}`); console.error(`Error ensuring directory exists: ${error.message}`);
throw error; throw error;
} }
} }
} }
}

28
main.ts
View file

@ -111,7 +111,6 @@ export default class SmartMemosPlugin extends Plugin {
// Add a new method to handle the audio recording and processing // Add a new method to handle the audio recording and processing
async handleAudioRecording(audioFile: Blob, transcribe: boolean) { async handleAudioRecording(audioFile: Blob, transcribe: boolean) {
try { try {
console.log('Handling audio recording:', audioFile); console.log('Handling audio recording:', audioFile);
if (!audioFile) { if (!audioFile) {
@ -121,7 +120,6 @@ export default class SmartMemosPlugin extends Plugin {
this.audioFile = audioFile; this.audioFile = audioFile;
// Save the audio recording as a .wav file // Save the audio recording as a .wav file
const fileName = `recording-${Date.now()}.wav`; const fileName = `recording-${Date.now()}.wav`;
const file = await saveFile(this.app, this.audioFile, fileName, this.appJsonObj.attachmentFolderPath); const file = await saveFile(this.app, this.audioFile, fileName, this.appJsonObj.attachmentFolderPath);
@ -142,6 +140,7 @@ export default class SmartMemosPlugin extends Plugin {
if (transcribe) { if (transcribe) {
this.transcribeRecording(file); this.transcribeRecording(file);
} }
// Handle the saved audio file // Handle the saved audio file
// You can replace this with your own handling logic // You can replace this with your own handling logic
console.log(file); console.log(file);
@ -300,25 +299,37 @@ async transcribeRecording(audioFile: TFile) {
async findFilePath(text: string, regex: RegExp[]) { async findFilePath(text: string, regex: RegExp[]) {
let filename = ''; let filename = '';
let result: RegExpExecArray | null; let result: RegExpExecArray | null;
// Find the filename using the provided regex patterns
for (const reg of regex) { for (const reg of regex) {
while ((result = reg.exec(text)) !== null) { while ((result = reg.exec(text)) !== null) {
filename = normalizePath(decodeURI(result[0])).trim(); filename = normalizePath(decodeURI(result[0])).trim();
} }
} }
if (filename == '') throw new Error('No file found in the text.'); if (filename === '') {
throw new Error('No file found in the text.');
}
// Use the attachment folder path from the plugin settings
// Check if the filename already contains the attachment folder path
let fullPath; let fullPath;
if (filename.includes(this.appJsonObj.attachmentFolderPath)) { if (filename.includes(this.appJsonObj.attachmentFolderPath)) {
fullPath = filename; fullPath = filename;
console.log('full path 1: ', fullPath);
} else { } else {
// Use the attachment folder path from the plugin settings
fullPath = this.appJsonObj.attachmentFolderPath + '/' + filename; // Ensure no leading or trailing slashes in the attachment folder path and filename
const folderPath = this.appJsonObj.attachmentFolderPath.replace(/\/$/, ''); // Remove trailing slash if any
const filePath = filename.replace(/^\//, ''); // Remove leading slash if any
// Construct the full path
fullPath = `${folderPath}/${filePath}`;
fullPath = normalizePath(fullPath); // Normalize the full path
console.log('fill path 2: ', fullPath);
} }
// Attempt to find the file in the vault
const file = this.app.vault.getAbstractFileByPath(fullPath); const file = this.app.vault.getAbstractFileByPath(fullPath);
if (file instanceof TFile) { if (file instanceof TFile) {
return file; return file;
@ -327,6 +338,7 @@ async transcribeRecording(audioFile: TFile) {
} }
} }
async generateText(prompt: string, editor: Editor, currentLn: number, contextPrompt?: string) { async generateText(prompt: string, editor: Editor, currentLn: number, contextPrompt?: string) {
if (prompt.length < 1) throw new Error('Cannot find prompt.'); if (prompt.length < 1) throw new Error('Cannot find prompt.');
if ( this.settings.apiKey.length <= 1) throw new Error('OpenAI API Key is not provided.'); if ( this.settings.apiKey.length <= 1) throw new Error('OpenAI API Key is not provided.');

View file

@ -1,7 +1,7 @@
{ {
"id": "smart-memos", "id": "smart-memos",
"name": "Smart Memos", "name": "Smart Memos",
"version": "1.1.0", "version": "1.1.1",
"minAppVersion": "0.15.0", "minAppVersion": "0.15.0",
"description": "Create personalized and intelligent analysis, summaries, and more for audio recordings that can be imported or spoken directly into a note", "description": "Create personalized and intelligent analysis, summaries, and more for audio recordings that can be imported or spoken directly into a note",
"author": "Evan Moscoso", "author": "Evan Moscoso",

View file

@ -1,6 +1,6 @@
{ {
"name": "smart-memos", "name": "smart-memos",
"version": "1.1.0", "version": "1.1.1",
"description": "Create personalized and intelligent analysis, summaries, and more for audio recordings that can be imported or spoken directly into a note", "description": "Create personalized and intelligent analysis, summaries, and more for audio recordings that can be imported or spoken directly into a note",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {