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> {
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 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('/');
let currentPath = '';
for (const part of parts) {
currentPath = currentPath ? `${currentPath}/${part}` : part;
try {
const folder = app.vault.getAbstractFileByPath(currentPath);
if (!folder) {
@ -36,8 +38,13 @@ async function directoryExistsAtPath(app: App, folderPath: string) {
throw new Error(`${currentPath} is not a folder`);
}
} 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}`);
throw error;
}
}
}
}

32
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
async handleAudioRecording(audioFile: Blob, transcribe: boolean) {
try {
console.log('Handling audio recording:', audioFile);
if (!audioFile) {
@ -121,7 +120,6 @@ export default class SmartMemosPlugin extends Plugin {
this.audioFile = audioFile;
// Save the audio recording as a .wav file
const fileName = `recording-${Date.now()}.wav`;
const file = await saveFile(this.app, this.audioFile, fileName, this.appJsonObj.attachmentFolderPath);
@ -142,6 +140,7 @@ export default class SmartMemosPlugin extends Plugin {
if (transcribe) {
this.transcribeRecording(file);
}
// Handle the saved audio file
// You can replace this with your own handling logic
console.log(file);
@ -152,7 +151,7 @@ export default class SmartMemosPlugin extends Plugin {
}
// Add a new method to transcribe the audio file and generate text
async transcribeRecording(audioFile: TFile) {
async transcribeRecording(audioFile: TFile) {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView) {
console.error('No active Markdown view found.');
@ -179,7 +178,7 @@ async transcribeRecording(audioFile: TFile) {
this.writing = false;
});
});
}
}
writeText(editor: Editor, LnToWrite: number, text: string) {
const newLine = this.getNextNewLine(editor, LnToWrite);
@ -300,25 +299,37 @@ async transcribeRecording(audioFile: TFile) {
async findFilePath(text: string, regex: RegExp[]) {
let filename = '';
let result: RegExpExecArray | null;
// Find the filename using the provided regex patterns
for (const reg of regex) {
while ((result = reg.exec(text)) !== null) {
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;
if (filename.includes(this.appJsonObj.attachmentFolderPath)) {
fullPath = filename;
console.log('full path 1: ', fullPath);
} 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);
if (file instanceof TFile) {
return file;
@ -327,6 +338,7 @@ async transcribeRecording(audioFile: TFile) {
}
}
async generateText(prompt: string, editor: Editor, currentLn: number, contextPrompt?: string) {
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.');

View file

@ -1,7 +1,7 @@
{
"id": "smart-memos",
"name": "Smart Memos",
"version": "1.1.0",
"version": "1.1.1",
"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",
"author": "Evan Moscoso",

View file

@ -1,6 +1,6 @@
{
"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",
"main": "main.js",
"scripts": {