osjs-client icon indicating copy to clipboard operation
osjs-client copied to clipboard

Add option interface for VFS readdir

Open mahsashadi opened this issue 3 years ago • 13 comments

Hi I plan to add gradual fetching of data by scrolling down in File Manger Application. Due to this issue, It seems default system adapter does not send options to the server correctly.

mahsashadi avatar Aug 24 '21 11:08 mahsashadi

NB: I changed the title of this issue

andersevenrud avatar Aug 24 '21 11:08 andersevenrud

I examined the code.

In addition to explicitly setting options to {} in this line, I think we are loosing the set options, in this line. Am I right?

mahsashadi avatar Aug 25 '21 09:08 mahsashadi

Ah, yes. I think I have an open issue about not properly encoding the options in GET requests. That needs to be looked at!

andersevenrud avatar Aug 26 '21 16:08 andersevenrud

In addition to replacing options : {} with options in this line, we can do the following changes in two mentioned files:

// osjs-client/src/utils/fetch.js 
// new encodeQueryData implementation

const encodeQueryData = (data, nesting = '') => {
  const pairs = Object.entries(data).map(([key, val]) => {
    if (typeof val === 'object') {
      return encodeQueryData(val, nesting + `${key}.`);
    } else {
      return encodeURIComponent(nesting + key) + '=' + encodeURIComponent(val);
    }
  });
  return pairs.join('&');
};
// osjs-server/src/vfs.js 
// new createOptions implementation
const createOptions = req => {
  const options = req.fields.options ? req.fields.options : decodeOptions(req.fields);
  // the rest code
}
const decodeOptions = (reqFileds) => {
  let options = {};
  Object.keys(reqFileds).map( item => {
      let keyPath = item.split(".");
      assignObject(options, keyPath,reqFileds[item]);
  })
    return options
}

const assignObject = (obj, keyPath, value) => {
  let lastKeyIndex = keyPath.length - 1;
  for (let i = 0; i < lastKeyIndex; ++i) {
    let key = keyPath[i];
    if (!(key in obj)) {
      obj[key] = {};
    }
    obj = obj[key];
  }
  obj[keyPath[lastKeyIndex]] = value;
};

If you agree with these implementations, I can make pull requests.

mahsashadi avatar Aug 28 '21 12:08 mahsashadi

Open up a PR and I will do a code review there :) I have some comments on the server integration there.

andersevenrud avatar Aug 28 '21 15:08 andersevenrud

https://github.com/os-js/osjs-client/pull/164 https://github.com/os-js/osjs-client/pull/165 https://github.com/os-js/osjs-server/pull/56

mahsashadi avatar Aug 29 '21 06:08 mahsashadi

Thanks! I was going to look at these today, but I've been at work all day and now realize it's almost midnight :sweat_smile: I'll check it out ASAP!

andersevenrud avatar Aug 29 '21 21:08 andersevenrud

As an example, below object is options in server-side vfs adapter, sending from file-manager. So by this encoding, the options you set here, are accessible by options.options

{
  path: 'home:/New directory',
  options: { showHiddenFiles: 'false' },
  session: {
    // the session
  }
}

Maybe the result should be somthing like this!

{
  path: 'home:/New directory',
  showHiddenFiles: 'false' ,
  session: {
    // the session
  }
}

mahsashadi avatar Sep 01 '21 12:09 mahsashadi

Sorry, but I don't understand what you mean by the last comment.

andersevenrud avatar Sep 01 '21 13:09 andersevenrud

I just want to ask you if I do the encoding of options in GET url in a correct way.

No problem, of course you will check its correctness when you test the PRs.

mahsashadi avatar Sep 01 '21 16:09 mahsashadi

No problem, of course you will check its correctness when you test the PRs.

I will add unit tests for everything so that we can confirm that it works in all scenarios :) If you know how to do that, feel free to add them yourself.

andersevenrud avatar Sep 01 '21 16:09 andersevenrud

Great, Thanks a lot. I hope you could find time to check these PRs out.

mahsashadi avatar Sep 01 '21 17:09 mahsashadi

I have left reviews in all PRs.

andersevenrud avatar Sep 01 '21 17:09 andersevenrud