Automatically coming slashes with FileStream in Monodroid - filestream

I have been working on a project using Monodroid. I need to use FileStream to access a file in my project. I write the path as a first argument in FileStream like FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read) but two slashes come automatically from FileStream, therefore the path is not valid. For example, if the path is "res/raw/aFile.txt", after FileStream method it becomes "//res/raw/aFile.txt" and an exception occurs. Because of the exception, I cannot split and get the valid path. How can I get rid of the two slashes that automatically coming from the FileStream method?
Regards.

If you're trying to open a raw resource file, I would suggest using the OpenRawResource() method instead:
using (var stream = Resources.OpenRawResource(Resource.Raw.File))
{
}

Related

TYPO3 11: convert t3 file uri into file identifier

How can I convert the TYPO3 file uri t3://file?uid=54 to be usable for other TYPO3 methods which need a file identifier?
The file uri is returned by a flexform which selects a XML file. This XML file should be read.
However I cannot find a useful API function in the TYPO3 Core.
$paramTestFile = 't3://file?uid=54';
$xmlString = GeneralUtility::getURL($paramTestFile);
The above code fails for TYPO3 URIs.
The file reference with uid=54 is at "fileadmin/example.xml".
The Filelist backend module shows the details of this file.
However I need this file path also in the PHP code in order to read in the file.
use TYPO3\CMS\Core\LinkHandling\FileLinkHandler;
$fileHandler = GeneralUtility::makeInstance(FileLinkHandler::class);
$fileInfo = $fileHandler->asString($paramTestFile);
It is not possible to use $paramTestFile in the above example.
The class FileLinkHandler and its method asString does exactly the opposite of what I need.
$content = #file_get_contents($url);
However the absolute file path is needed for the read method . How can I convert the FAL file URI into the file identifier?
use TYPO3\CMS\Core\Resource\StorageRepository;
$storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
$defaultStorage = $storageRepository->getDefaultStorage();
$fileInfo = $defaultStorage->getFileByIdentifier($paramTestFileIdentifier);
To resolve a t3://file... URN use TYPO3\CMS\Core\LinkHandling\LinkService::resolve(). This method will return an array with a key file containing a TYPO3\CMS\Core\Resource\FileInterface. This also works for other URNs but will return another array structure.
Then use the getContents() method of the FileInterface to finally get the content of the file.
The documentation is not very detailed for this part of the core so I have linked you directly to the related sources.

Dropbox API overwrite upload files instead of rename it

I am trying to upload some files to dropbox using their java API (version 2-beta-4), but some of these files have the same name.
What I would like to know is: What is the reason for I send a file (for instance "file.txt") to dropbox, this file is uploaded, but if I send another file with the same name (file.txt) dropbox overwrite the old file with this new one instead of renaming it to "file (1).txt", even I am setting autorename true and the WriteMode to add?
Code:
DbxRequestConfig config = new DbxRequestConfig("sample", "pt_BR");
String acessToken = "...";
client = new DbxClientV2(config, accessToken);
InputStream input = new ByteArrayInputStream(file.getBytes());
FileMetadata file = client.files.uploadBuilder(path).mode(WriteMode.add).autorename(true)
.mute(true).run(input);
Thanks.
WriteMode.add is what's causing this behavior. "Add" means "Add a new file with this name," so it never overwrites an existing file. If you want to overwrite the existing file, use WriteMode.overwrite.
(Also, isn't it WriteMode.add() and WriteMode.overwrite()? I thought those were methods.)

Include file as input stream in scala

I'm having problems with using a file in my code. I'm working with javaPNS and trying to get it to work. What I think is wrong now is how I get my file for the keystore parameter in the Push.alert method in javaPNS.
What I'm really wondering is how do I "get a resource" properly so it has the correct format?
Here's the exception that is thrown:
javapns.communication.exceptions.InvalidKeystoreReferenceException:
Invalid keystore parameter (null). Must be InputStream, File, String (as a file path),
or byte[].
at........
And here's how I currently include the file:
val keystoreFile = getClass.getResourceAsStream("/app/conf/cert.p12")
If we look at the exception I get, do you think this above is the problem? How should I include the file properly in Scala?
regards,
The problem you are seeing has to do with the fact that the InputStream you are loading is coming back as null. This happens when the file you are trying to load can not be found on the class path. Try changing your code to:
getClass.getClassLoader.getResourceAsStream("app/conf/cert.p12")
You will notice I removed the leading slash on the file path as I believe this is your issue. Then just make sure that whatever the parent directory to "app" is is on your class path.

What process is Streamreader using?

I made a simple program with
StreamReader sr = new StreamReader(File.OpenRead(dlg.FileName));
txtBox1.Text = sr.ReadToEnd();
and forgot to put
sr.Dispose();
and now when I try to run the program and open a file I get the IOException was unhandled error message that says "The process cannot access the file because it is being used by another process." So my question is, does anyone know what process is it using? I would like to be able to find it in the taskmanager and end it instead of writing a bunch of exception handling code since this is just a program that I'm using for practice.
The file I tried to open is a txt file in MyDocuments.
The process that is using the file is the process that opened it which is very likely .vshost.exe if you are running this from from Visual Studio.
To avoid 'forgetting' to dispose of objects in the future using the using statementfor anything implementing IDisposable (which StreamReader does). Your code would look like
using(StreamReader sr = new StreamReader(File.OpenRead(dlg.FileName)))
{
txtBox1.Text = sr.ReadToEnd();
}
I got the same error when I was using a streamreader to find out the end of the text in the file so I could then add the new text after the text in the file with the writeline method. My Documents has accessibility issues when it come to development anyway. Try putting the .txt into some other location maybe /temp folder?

Private assets in Play 2.1

In a Play 2.1 application, where is the proper place to store private assets?
By "private asset", I mean a data file that is used by the application but not accessible to the user.
For example, if I have a text file (Foo.json) that contains sample data that is parsed every time the application starts, what would be the proper directory in the project to store it?
Foo.json needs to be included in the deployment, and needs to be uniformly accessible from the code in both development and production.
Some options:
Usually the files goes to conf folder. ie: conf/privatefiles/Foo.json
If they are subject of often change you can consider adding to your application.conf path to the external folder somwhere in the filesystem (full path), in such case you'll be able to edit the content easily without redeploying the apps: /home/scrapdog/privatefiles/Foo.json
You can store them in database as well, benefits are the same as in previous option - easy editing.
In all cases consider using memory cache to avoid reading it from filesystem/database every time when required.
I simply use a folder called data at the application root. You can use the name you want or better, store the actual name in the configuration file.
To resolve its path, I use the following snippet:
lazy val rootPath = {
import play.api.Play.current
play.api.Play.application.path.getPath
}
lazy val dataPath = rootPath + "/data/"
You can do what I did, I got the answer from #Marius Soutier here. Please upvote his answer there if you like it:
You can put "internal" documents in the conf folder, it's the equivalent to resources in standard sbt projects.
Basically create a dir under conf called json and to access it, you'd use Play.resourceAsStream(). Note that this gives you a java.io.InputStream because your file will be part of the JAR created by activator dist.
My example is using it in a view but you can modify it as you want.
Play.resourceAsStream("json/Foo.json") map { inputStream =>
Ok(views.html.xxx(XXX.do_something_with_stream(inputStream)))
} getOrElse (InternalServerError)
You can also use Play.resource(), this will give you a java.net.URL, you can use getFile() to get the java.io.File out of it.
Play.resource("json/Foo.json") map { fileURL =>
Ok(views.html.xxx(XXX.do_something_with_file(fileURL.getFile())))
} getOrElse (InternalServerError)