ProxymanApp / Proxyman

Modern. Native. Delightful Web Debugging Proxy for macOS, iOS, and Android ⚡️
https://proxyman.io
5.34k stars 177 forks source link

how to readFile before writeToFile() #2023

Open andforce opened 1 month ago

andforce commented 1 month ago

I understand that ProxyMan provides the functionality to save response.body to a file using thewriteToFile() function, but I seem to have not found a function to read files.

I want to add some conditions before writing to the file, such as whether the file exists and the content of the file, to determine whether I want to save the file. In short, I don't want to directly overwrite the existing file.

Here is some pseudocode, as I have not found a relevant implementation method.

async function onResponse(context, url, request, response) {

  const filename = request.queries.page;

  const path = "~/Desktop/" + filename + ".json"

  if ("path not exists or content is different from ${response.body}") {
    writeToFile(response.body, path);
  } else {
    // just print some log
  }

  return response;
}
NghiaTranUIT commented 1 month ago

Thanks for your input. I guess I can support a simple version of isFileExist() and readFile() for you 👍

andforce commented 1 month ago

Thanks for your input. I guess I can support a simple version of isFileExist() and readFile() for you 👍

I'm very much looking forward to your new version. I wonder if it will support using JSON parsing to read the contents of files. This way, I can compare file contents more conveniently and specifically.

async function onResponse(context, url, request, response) {

  const filename = request.queries.page;

  const path_to_local_file = "~/Desktop/" + filename + ".json"
  if ("path_to_local_file not exists or path_to_local_file content is empty") {
     writeToFile(response.body, path_to_local_file);
  } else {
       var local_file_json = JSON.parse(path_to_local_file's content)
       if (local_file_json.some_value != response.body.some_value) {
           // to update local file
           writeToFile(response.body, path_to_local_file);
       } else {
           // no nothing or just print some log
       }
  }

  return response;
}
NghiaTranUIT commented 1 month ago

@andforce you can try this Beta build: https://download.proxyman.io/beta/Proxyman_5.3.0_New_file_common_func.dmg

async function onResponse(context, url, request, response) {

  const textFilePath = "~/Desktop/myfile.json";

  // check exist
  if (isFileExists(filePath)) {

    // read from file
    const text = readFromFile(textFilePath);

    // pares string to JSON Object
    const obj = JSON.parse(text);
  }

  // Read binary file
  const binaryFilePath = "~/Desktop/screenshot.png";
  const binaryFile = readFromFile(binaryFilePath); // Uint8Array

  // Done
  return response;
}
andforce commented 1 month ago

@NghiaTranUIT Thank you very much; I am amazed by your efficiency. I tried the new beta version you provided, it works fine~

NghiaTranUIT commented 1 month ago

Awesome, glad it helps you 👍