Delete files in applicationDataDirectory - caching

In titanium, what is the best way to delete -specific- files in the applicationDataDirectory?

The best way will be simply:
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
if ( file.exists() ) {
file.deleteFile();
}
For complete details Titanium.Filesystem.File.

Deleting a file is this simple ,
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'delete_me.txt');
f.write('foo');
// make sure there's content there so we're sure the file exists
// Before deleting, maybe we could confirm the file exists and is writable
// but we don't really need to as deleteFile() would just return false if it failed
if (f.exists() && f.writeable) {
var success = f.deleteFile();
Ti.API.info((success == true) ? 'success' : 'fail'); // outputs 'success'
}
One of the best material on FileSystems in simple: https://wiki.appcelerator.org/display/guides/Filesystem+Access+and+Storage#FilesystemAccessandStorage-Deletingfiles

Related

NativeScript: How to copy a file from an apps folder to a user accessible folder?

I want to copy storage.db to documents or downloads folder. It's very easy to get the file path:
const filePath = application.android.context.getDatabasePath("storage.db").getAbsolutePath();
But, what isn't that easy is to copy that file to a folder users have access to. I searched this whole forum, and I found nothing useful for my case.
I'm using NativeScript 4.0.1 with vanilla JS.
If you want to share the DB file, the easiest way is to use nativescript-share-file plugin, send the file path and it will give you a nice dialog with intent picker, user may choose to Email the file Or save it to local folder etc.,
const shareFile = new ShareFile();
shareFile.open({
path: filePath,
});
I finally found the solution. I've seen so many users trying to achieve this, and I hope this will help all of you.
Add this to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Install nativescript-permissions:
npm i nativescript-permissions
Asking for permission:
const permissions = require('nativescript-permissions');
permissions.requestPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, "");
Require the necessary modules:
const fileSystemModule = require("tns-core-modules/file-system");
const application = require("application");
Then, create this function where you need to use it:
function copyFile() {
var myInput = new java.io.FileInputStream(appModule.android.context.getDatabasePath("storage.db").getAbsolutePath());
var myOutput = new java.io.FileOutputStream("/storage/emulated/0/databases/storage.db");
try {
var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.class.getField("TYPE").get(null), 1024);
var length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
}
catch (err) {
console.info("Error", err);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
exports.copyFile = copyFile;
In my case, the file storage.db will be copied to /storage/emulated/0/databases. If you need to create a folder, just do the following:
try {
var javaFile = new java.io.File("/storage/emulated/0/newfolder");
if (!javaFile.exists()) {
javaFile.mkdirs();
javaFile.setReadable(true);
javaFile.setWritable(true);
}
}
catch (err) {
console.info("Error", err);
}
If the destination folder has a file with the same name as the one you want to copy, you need to remove it first. That's why you should create a specific folder to guarantee it's empty.

Deleting file which was uploaded with $request->file->store

For some reason I am not able to delete a file I upload with $request->file->store.
I tried \File::delete, \File::Delete, \Storage::Delete, and \Storage::delete. I want to avoid using unlink as that is not the laravel way.
May you please help me?
This is my code:
if ($user->avatar_path) {
// delete old one
\File::delete(app_path().$avatar_path);
}
$avatar_path = $request->file('avatar')->store('uploads/users/'.$user->id.'/avatar', 'public');
$user->update($request->except('avatar') + ['avatar_path' => $avatar_path]);
We see here that I check if there is an old avatar, and then I want to delete it. Then I upload the next one and update the database with the new path.
Finally solved this. I had to use Storage::disk('public')->delete($old_avatar_path);. Final code:
if ($request->has('avatar')) {
$old_avatar_path = $user->avatar_path;
$avatar_path = $request->file('avatar')->store('avatars', 'public');
$user->update($request->except('avatar') + ['avatar_path' => $avatar_path]);
if ($old_avatar_path) {
// delete old one
Storage::disk('public')->delete($old_avatar_path);
}

Laravel Collective SSH results

I am performing SSH in Laravel whereby I connect to another server and download a file. I am using Laravel Collective https://laravelcollective.com/docs/5.4/ssh
So, the suggested way to do this is something like this
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path);
if($result) {
return $path;
} else {
return 401;
}
Now that successfully downloads the file and moves it to my local server. However, I am always returned 401 because $result seems to be Null.
I cant find much or getting the result back from the SSH. I have also tried
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path, function($line){
dd( $line.PHP_EOL);
});
But that never gets into the inner function.
Is there any way I can get the result back from the SSH? I just want to handle it properly if there is an error.
Thanks
Rather than rely on $result to give you true / false / error, you can check if the file was downloaded successfully in another way:
// download the file
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path);
// see if downloaded file exists
if ( file_exists($path) ) {
return $path;
} else {
return 401;
}
u need to pass file name also like this in get and put method:
$fileName = "example.txt";
$get = \SSH::into('scripts')->get('/remote/somelocation/'.$fileName, base_path($fileName));
in set method
$set = \SSH::into('scripts')->set(base_path($fileName),'/remote/location/'.$fileName);
in list
$command = SSH::into('scripts')->run(['ls -lsa'],function($output) {
dd($output);
});

Cloud code: can I use other files besides main.js?

Does all my cloud clode have to be in main.js or can I create other logical files and somehow add them into my cloud code?
If you just want to split your Parse.Cloud functions in multiple files, you also can do that and just require the file in your main.js.
So you can have this in your cloud/main.js
require('cloud/userFunctions.js')
and have all the related functions in your cloud/userFunctions.js
Parse.Cloud.define("processUserRequest", function(request, response) {
// do your stuff
});
You can split your code into different files using require.
The example below come from Parse documentation
If you have some code in cloud/name.js
var coolNames = ['Ralph', 'Skippy', 'Chip', 'Ned', 'Scooter'];
exports.isACoolName = function(name) {
return coolNames.indexOf(name) !== -1;
}
You can then you it in cloud/main.js
var name = require('cloud/name.js');
name.isACoolName('Fred'); // returns false
name.isACoolName('Skippy'); // returns true;
name.coolNames; // undefined.

CakePHP Cache Not Writing File

I cannot figure this one out ... that means it's got to be an easy one, right?!
Problem: Cake not writing files to file cache.
Configuration:
//app/Config/core.php
Configure::write('debug', 0);
Configure::write('Cache.disable', true);
Configure::write('Cache.check', true);
//app/Config/bootstrap.php
Cache::config('default', array('engine' => 'File'));
//app/Controller/CategoriesController.php, 'view' method:
public function view($id = null) {
$this->helpers[] = 'Format';
$this->helpers[] = 'Cache';
$this->cacheAction = "3600";
....
}
Perms on the app/tmp/cache/* are 0777. world writable.
But still not files being written to cache .... what am I missing??
DJC
Configure::write('Cache.disable', true);
You don't know why the cache is not being written...?

Resources