Proxyman icon indicating copy to clipboard operation
Proxyman copied to clipboard

how to readFile before writeToFile()

Open andforce opened this issue 1 year ago • 5 comments

I understand that ProxyMan provides the functionality to save response.body to a file using the writeToFile() 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;
}

andforce avatar May 13 '24 13:05 andforce

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

NghiaTranUIT avatar May 14 '24 01:05 NghiaTranUIT

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;
}

andforce avatar May 14 '24 02:05 andforce

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

  • Add: isFileExists()
  • Add: readFromFile()
  • Snippet Code: https://docs.proxyman.io/scripting/snippet-code#read-a-file
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;
}

NghiaTranUIT avatar May 14 '24 03:05 NghiaTranUIT

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

andforce avatar May 14 '24 06:05 andforce

Awesome, glad it helps you 👍

NghiaTranUIT avatar May 14 '24 07:05 NghiaTranUIT