I want to use chrome.fileSystem API to create a new file in C:/ or anywhere, but I cannot figure out how to use this API.
I cannot find any argument for file path, only thing is fileEntry, but how can I generate fileEntry with something like C://a/b/c?
Chrome apps have limitations - for security reasons - on what files can be accessed. Basically, the user needs to approve access from your app to the files and directories that are accessed.
The only way to get access to files outside of the app's sandbox is through a user gesture - that is, you need to ask the user for a file. You do this with chrome.fileSystem.chooseEntry.
If this isn't clear of obvious, maybe you could explain more about what you are trying to do with the files and we can give advice on the best way to do this. Usually chrome.fileSystem is not the best choice for data storage - there are other more convenient and sandboxed alterntives like chrome.storage.
It's a bit tricky to work with. But it follows the same model other languages use, except it's even tricker because of all the callbacks. This is a function I wrote to get a nested file entry, creating directories as it goes along. Maybe this can help you get started.
For this function, youd pass in a FileSystem that you'd get from something like chrome.fileSystem.chooseEntry with a directory type option, and path would be in your example ['a','b','c']
function recursiveGetEntry(filesystem, path, callback) {
function recurse(e) {
if (path.length == 0) {
if (e.isFile) {
callback(e)
} else {
callback({error:'file exists'})
}
} else if (e.isDirectory) {
if (path.length > 1) {
e.getDirectory(path.shift(), {create:true}, recurse, recurse)
} else {
e.getFile(path.shift(), {create:true}, recurse, recurse)
}
} else {
callback({error:'file exists'})
}
}
recurse(filesystem)
}
Related
Can someone please help me with this, am trying to use OpenFileDialog class from System.Windows.Forms to open a file dialog and read the selected file. Then, this error showed up. I've referenced it but still the same, below is the code.
`using UnityEngine
using UnityEngine.UI
using System.Windows.Forms;
public class OpenFileButtonScript : MonoBehaviour
{
public TextFieldScript textFieldScript;
public void OpenFile()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
string text = System.IO.File.ReadAllText(filePath);
textFieldScript.inputField.text = text;
}
}
}`
It may look like you have access to all of the native Window system libraries, but it just looks like it. In actuality, a lot of the time you're simply given stubs, or shims, that look like the full Window libraries, because there's a certain element that Unity wants to use from those namespaces. If you think about it, the code you present above, what do you think it should do on Android or Nintendo devices? The simple answer is, it simply won't work.
Generally in cases like this, you have to gain access to the native operating system, and perform those calls directly. For example, there is a file browser asset on the Asset Store, that does this for you. It's not free, because the process isn't trivial.
Depending on how much effort you want to put in, you CAN read files from the local file stores (to varying degrees based on platform). It's possible to read the list of files in a location, and use either uGUI or UIToolkit to create your own File Open Dialogue box. Again, this isn't a trivial task either. So you have to be sure that you'd want to go down that path.
My (non-sandboxed) app has an embedded XPC helper which runs as root.
I would like to reference the (real) user's home directory from inside my helper, but these usual suspects simply return /var/root:
FileManager.default.homeDirectoryForCurrentUser
NSHomeDirectory()
I can't simply pass Users/bob to my helper for security reasons — if an exploit managed to call my helper method with any URL it wished, and my helper did things based on that as root, I fear bad things could be achieved.
As vadian commented there are fundamental conceptual issues with what you're asking. What you probably actually want to do is be sure the process communicating with your helper tool is in fact trusted.
To do that you need to use SecCodeCreateWithXPCMessage and then use the resulting SecCode instance to validate the caller. For an example of how to do that, take a look at the acceptMessage function in the SecureXPC framework.
EDIT: Turns out there is a way to do this that does work from a Command Line Tool such as one installed with SMJobBless. This answer is adapted from Apple's Technical Q&A QA1133.
If you for whatever reason want to ignore the above, there's an approach you can take which may produce unexpected results if multiple users have active consoles. From Apple's documentation for SCDynamicStoreCopyConsoleUser: "Note that this function only provides information about the primary console. It does not provide any details about console sessions that have fast user switched out or about other consoles."
import SystemConfiguration
extension FileManager {
var homeDirectoryForConsoleUser: URL? {
var homeDirectory: URL?
if let consoleUser = SCDynamicStoreCopyConsoleUser(nil, nil, nil) as String?,
consoleUser != "loginwindow" {
homeDirectory = URL(fileURLWithPath: "/Users/\(consoleUser)")
}
return homeDirectory
}
}
And then you can make use of this anywhere in your helper tool:
if let homeDirectory = FileManager.default.homeDirectoryForConsoleUser {
// Do something useful here
}
I am customizing ICN (IBM Content Navigator) 2.0.3 and my requirement is to restrict user to upload files over 10mb and only allowed files are .pdf or .docx.
I know I have to extend / customize the AddContentItemDialog but there is very less detail on exactly how to do it, or any video on it. I'd appreciate if someone could guide.
Thanks
I installed the development environment but I am not sure how to extend the AddContentItemDialog.
public void applicationInit(HttpServletRequest request,
PluginServiceCallbacks callbacks) throws Exception {
}
I want to also know how to roll out the changes to ICN.
This can be easily extended. I would suggest to read the ICN red book for the details on how to do it. But it is pretty standard code.
Regarding rollout the code to ICN, there are two ways:
- If you are using plugin: just replace the Jar file on the server location and restart WAS.
- If you are using EDS: you need to redeploy the web service and restart WAS.
Hope this helps.
thanks
Although there are many ways to do this, one way indeed is tot extend, or augment the AddContentItemDialog as you qouted. After looking at the (rather poor IBM documentation) i figured you could probably use the onAdd event/method
Dojo/Aspect#around allows you to do exactly that, example:
require(["dojo/aspect", "ecm/widget/dialog/AddContentItemDialog"], function(aspect, AddContentItemDialog) {
aspect.around(AddContentItemDialog.prototype, "onAdd", function advisor(original) {
return function around() {
var files = this.addContentItemGeneralPane.getFileInputFiles();
var containsInvalidFiles = dojo.some(files, function isInvalid(file) {
var fileName = file.name.toLowerCase();
var extensionOK = fileName.endsWith(".pdf") || fileName.endsWith(".docx");
var fileSizeOK = file.size <= 10 * 1024 * 1024;
return !(extensionOK && fileSizeOK);
});
if (containsInvalidFiles) {
alert("You can't add that :)");
}else{
original.apply(this, arguments);
}
}
});
});
Just make sure this code gets executed before the actual dialog is opened. The best way to achieve this, is by wrapping this code in a new plugin.
Now on creating/deploying plugins -> The easiest way is this wizard for Eclipse (see also a repackaged version for newer eclipse versions). Just create a new arbitrary plugin, and paste this javascript code in the generated .js file.
Additionally it might be good to note that you're only limiting "this specific dialog" to upload specific files. It would probably be a good idea to also create a requestFilter to limit all possible uses of the addContent api...
I'm trying to scrape products from amazon (shoes to be precise) such as from here: http://www.amazon.com/DC-Mens-Skate-Black-Plaid/dp/B005BWAQVU/ref=sr_1_1?ie=UTF8&qid=1333376200&sr=8-1
For some reason the images no longer save and download. I am afraid I may have incorrect parameters for the images.
Here is the excerpt from my code in which that part takes place:
sub get_data
{
my($product_content,$gender,$product_category,$prod_tag,$sub_category)=#_;
my($product_name,$product_code,$brand,$product_price,$image_file,$image_name,$prod_size,$size_name,$color_name,$prod_color);
if($product_content=~m/<div\s*id\=\"atfResults\"[^>]*>([\w\W]+?)<div\s*id\=\"centerBelowStatic\">/is)
{
my $block=$1;
while($block=~m/<div\s*class\=\"image\">\s*<a[^>]*?href\=\"([^>]+?)\"[^>]*>\s*<img[^>]*>/igs)
{
my $source_url=$1;
$source_url=URI::URL->new_abs($source_url,$home_url);
my $final_content=&get_cont($source_url,$home_url,'GET');
if($final_content=~m/<h1[^>]*>\s*<[^>]*>\s*([^>]+?)\s*</is)
{
$product_name=decode_entities($1);
print "\n\n$count :: Product Name :: $product_name\n";
$product_name=~s/\'/\'\'/igs;
}
if($source_url=~m/\/dp\/([^>]+?)\//is)
{
$product_code=$1;
$product_code=~s/\'/\'\'/igs;
}
if($final_content=~m/<span\s*class\=\"brandLink\">\s*<[^>]*>\s*([^>]+?)\s*</is)
{
$brand=decode_entities($1);
print "Product Brand :: $brand\n";
$brand=~s/\'/\'\'/igs;
}
if($final_content=~m/<td\s*class\=\"priceBlockLabelPrice\">\s*Price\s*\:\s*<[^>]*>\s*<[^>]*>\s*<[^>]*>\s*([^>]+?)\s*</is)
{
$product_price=$1;
$product_price=~s/\'/\'\'/igs;
}
if($final_content=~m/<script[^>]*>\s*var\s*colorImages\s*\=\s*\{([\w\W]+?)\]\};/is)
{
my $color_block=$1;
my $col=1;
$image_file="";
$image_name="";
while($color_block=~m/\"large\"\:\[\"([^>]+?)\"/igs)
{
my $img_src=$1;
if($img_src=~m/(?:.+\/)([^>]*?\.[a-z]+)/is)
{
my $img_fname=$1;
getstore($img_src,"Images/$img_fname");
$img_fname=$dir."/Images/$img_fname";
$image_name=$image_name."Product_Image_filename_".$col.",";
$img_fname=~s/\'/\'\'/igs;
$image_file=$image_file."\'$img_fname\',";
$col++;
}
undef($img_src);
last if($col>10);
}
undef($color_block);
}
Everything else seems to save fine, but the images, nada. I'm not really a perl expert either so if it's something obvious, forgive me.
Why would you scrape their site when Amazon provides an API for getting hold of their product details?
You should use WWW::Scripter module for that. Today morning a new version of this module has been released and one of very new features of this new version is image fetching. The module will fetch images with proper referer and cookies (if apply), so you should have no problem to capture images...
Use Firefox and install an add on "Disable HTTP referer at Startup". Then restart firefox and try again. You will get images.
I developing an application in zend framework. I want to store certain secure images and pdf documents outside public folder like /project/data/uploads or /projects/application/data/uploads.
I am not able to access the images/pdf documents other than public folder.
Can someone suggest a way to do it.
thank you
You have to have a separate action that knows how to fetch and deliver all that stuff. Something like this:
public function viewpdfAction()
{
$id = (int) $this->_getParam('id', 0);
// You implement some function - either here in your controller
// or someplace else - to get the pdf name from the passed id.
// Alternatively, you can pass the name itself. Up to you, of course.
// The key is that the request somehow identifies the file requested.
$pdfName = $this->_mapPdfIdToName($id);
// The path to the real pdf
$pdfFile = '/project/data/uploads/pdf/' . $pdfName;
// Disable rendering
$this->_helper->viewRenderer->setNoRender(true);
// Send the right mime headers for the content type
$this->getResponse()
->setBody('')
->setHeader('Cache-control', 'public') // needed for IE, I have read
->setHeader('Content-type', 'application/pdf')
->setHeader('Content-Disposition', sprintf('attachment; filename="%s"', $pdfName));
// Send the content
readfile($pdfFile);
}
Of course, some of this can be pushed down into service classes to keep the controller as thin as possible. Everyone has different tastes in this regard.
I confess that this code not completely tested, mostly trying to give the basic idea. If I have made a total bonehead error in here, please let me know.
Hope it helps!