Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to readFile before writeToFile() #2023

Open
andforce opened this issue May 13, 2024 · 5 comments
Open

how to readFile before writeToFile() #2023

andforce opened this issue May 13, 2024 · 5 comments
Assignees
Labels
✅ Done Ticket is addressed and fixed. question Further information is requested

Comments

@andforce
Copy link

andforce commented May 13, 2024

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 andforce added the question Further information is requested label May 13, 2024
@NghiaTranUIT NghiaTranUIT self-assigned this May 14, 2024
@NghiaTranUIT
Copy link
Member

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

@andforce
Copy link
Author

andforce commented May 14, 2024

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
Copy link
Member

NghiaTranUIT commented May 14, 2024

@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
Copy link
Author

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

@NghiaTranUIT NghiaTranUIT added the ✅ Done Ticket is addressed and fixed. label May 14, 2024
@NghiaTranUIT NghiaTranUIT added this to the Proxyman 5.4.0 milestone May 14, 2024
@NghiaTranUIT
Copy link
Member

Awesome, glad it helps you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✅ Done Ticket is addressed and fixed. question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants