Skip to content

loadStrings String Addition/Concatenation in Filepath #839

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

Closed
Sunomikey opened this issue Feb 21, 2019 · 6 comments
Closed

loadStrings String Addition/Concatenation in Filepath #839

Sunomikey opened this issue Feb 21, 2019 · 6 comments
Labels
Good First Issue A beginner-friendly issue, great for first-time contributors Help Wanted Would love additional input or contributions! Priority:High Should be addressed soon but not critical

Comments

@Sunomikey
Copy link

Sunomikey commented Feb 21, 2019

Nature of issue?

  • Found a bug

Details about the bug:

loadStrings() won't load anything past a direct file path.

What I can do:
loadStrings('test.txt')

OR
x = 'testFolder/test.txt';
loadStrings(x);

What I can't do:
ext = '.txt';
loadStrings('test'+ext');

OR
i = "1";
loadStrings('test'+i+'.txt');

  • Web browser and version: Chrome p5 Web Editor 72.0.3626.109 (Official Build) (64-bit)
  • Operating System: Windows
  • Steps to reproduce this bug:
  1. Have file called "test.txt"

2:

function preload(){
	let ext = ".txt"
	txt = loadStrings("test"+ext);
}

It won't ever leave the preload as it can't find the file. Is there a variable type it's looking for?

@Sunomikey Sunomikey changed the title loadStrings String Addition/Concatenation loadStrings String Addition/Concatenation in Filepath Feb 21, 2019
@catarak
Copy link
Member

catarak commented Feb 21, 2019

thanks for reporting! i'm not able to reproduce this, for me it is working, see https://editor.p5js.org/cassie/sketches/tk22ndoWs

@Sunomikey
Copy link
Author

Hi @catarak!

That was working for me as well. My problem came with adding strings together to make a filepath.

Modifying your code would be:

let txt;
let fileName = 'test';

function preload() {
	txt = loadStrings(fileName+'.txt'); 
}

function setup() {
  createCanvas(400, 400);
  print(txt);
}

function draw() {
  background(220);
}

@meiamsome
Copy link
Member

This is the same underlying issue as #813 I believe, that you can't refer to local files properly. #771 #674 also seem at least partially related. All of these cases are referring to local files in the code that aren't being served up correctly.

For example, in @catarak 's sketch above the file at https://editor.p5js.org/cassie/sketches/tk22ndoWs/test.txt returns a 404 instead of the correct contents? Do we expect that to return the text content like it returns for images?

@catarak
Copy link
Member

catarak commented Feb 21, 2019

ahh sorry! did not read closely enough 😸

and @meiamsome, yes, you're right. that url should return the text content like it does for images.

@catarak catarak added type:bug Help Wanted Would love additional input or contributions! Priority:High Should be addressed soon but not critical Good First Issue A beginner-friendly issue, great for first-time contributors labels Feb 21, 2019
@Sunomikey
Copy link
Author

Thanks @meiamsome for linking the other issues. The definition of "When the file names are generated programmatically, rather than just a string inline, it loads the files in a different way." in 674 would be a good way to describe this.

Good to know I'm not crazy and that the example code "should" work.

Thanks for the help everyone!

@meiamsome
Copy link
Member

For anyone looking to work on this, I believe the issue is in this function:

export function getProjectAsset(req, res) {
Project.findById(req.params.project_id)
.populate('user', 'username')
.exec((err, project) => { // eslint-disable-line
if (err) {
return res.status(404).send({ message: 'Project with that id does not exist' });
}
if (!project) {
return res.status(404).send({ message: 'Project with that id does not exist' });
}
let assetURL = null;
const seekPath = req.params[0]; // req.params.asset_path;
const seekPathSplit = seekPath.split('/');
const seekFilename = seekPathSplit[seekPathSplit.length - 1];
project.files.forEach((file) => {
if (file.name === seekFilename) {
assetURL = file.url;
}
});
if (!assetURL) {
return res.status(404).send({ message: 'Asset does not exist' });
}
request({ method: 'GET', url: assetURL, encoding: null }, (innerErr, response, body) => {
if (innerErr) {
return res.status(404).send({ message: 'Asset does not exist' });
}
return res.send(body);
});
});
}

Specifically, it does not account for the case where file.content is used instead of file.url. I'll work on a fix for this in the next few days if someone else hasn't picked it up before then.

@Sunomikey Yes, I can see how this behaviour can be confusing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue A beginner-friendly issue, great for first-time contributors Help Wanted Would love additional input or contributions! Priority:High Should be addressed soon but not critical
Projects
None yet
Development

No branches or pull requests

3 participants