How to rename image file in flutter web from ImagePicker - flutter-web

I want to rename image file from ImagePicker in flutter web
I have tried various methods such as:
File picture = await ImagePicker.pickImage(
maxWidth: 800,
imageQuality: 10,
source: ImageSource.camera,
maxHeight: 800,
);
String dir = path.dirname(picture.path);
String newPath = path.join(dir, 'filerenamed.jpg');
picture.renameSync(newPath);
renameSync() doesn't seem to support for flutter web.
I get the error Error: Unsupported operation
PickedFile pickedFile = await _picker.getImage(source: ImageSource.camera);
String dir = (await getApplicationDocumentsDirectory()).path;
String newPath = path.join(dir, 'renamedFile.jpg');
File f = await File(pickedF.path).copy(newPath);
getApplicationDocumentsDirectory() doesn't seem to support web flutter either
Is there any other solution to rename file on web flutter ?

Related

VSCode extension can not find the file path on windows. "ENOENT: no such file or directory"

I try to get file in the VSCode extension project on Windows, but it return wrong path and error message:
Error: ENOENT: no such file or directory, lstat 'C:\F:\...'
I try to change path but it is not working.
// src\utils\index.ts
const isFile = async (input: string) => {
// input = input.match(/:\//) ? input.replace(/:\//, '://') : input;
input = input.match(/:\//) ? '/..' + input : input;
const stat = await fs.lstat(input);
return stat.isFile();
};
The project: vscode-imagemin
How can I fixed the problem?

Unzip a MacOS .app file in Electron using Node.js

I am trying to unzip a file called Restart.Manager.zip which contains a single item, Restart Manager.app. This code seems to unzip the file correctly but upon launching the outputted .app folder, I get an error "The application “Restart Manager” can’t be opened."
const JSZip = require('jszip');
const fs = require('fs');
const jetpack = require('fs-jetpack');
const originalFs = require('original-fs');
async function extractZip(filePath, destination) {
fs.readFile(filePath, function(err, data) {
if (!err) {
var zip = new JSZip();
zip.loadAsync(data).then(function(contents) {
Object.keys(contents.files).forEach(function(filename) {
const file = zip.file(filename);
if (file) {
file.async('nodebuffer').then(function(content) {
var dest = destination + '/' + filename;
if (filename.endsWith('.asar')) {
originalFs.writeFileSync(dest, content)
} else {
jetpack.write(dest, content);
}
});
}
});
});
}
});
};
extractZip('/Users/me/Desktop/Restart.Manager.zip', '/Users/me/Desktop')
Manually unzipping the .zip file creates a working .app so I'm not sure where the code is messing up.
Here is the file on GitHub releases for testing: https://github.com/itw-creative-works/restart-manager-download-server/releases/download/installer/Restart.Manager.zip but feel free to use your own zipped .app file (although it should probably be an Electron app in which case you can find one here https://www.electronjs.org/apps)
I have tried zipping things like a .png and it unzips fine, which makes me think it is having problems with .app files or possibly the fact that the .app contains a .asar file which Electron supposedly has problems handling when it comes to the fs module: https://github.com/electron/electron/issues/1658

change author of mp3 file

I'd like to set the author of a mp3 file based on the data sent by the user.
So far, I managed to get the data sent by the user (hardly an exploit anyway), but I can't manage to change the author of the file. I've tried using both node-id3 package and ffmetadata package, as suggested by this answer, but none of this worked.
The node-id3 approach
Here is a part of code that I wrote for the node-id3 approach, and whille the tags shown in readTags are indeed the ones that I added in the update method, it doesn't change the author of the file when I read it on my computer (with itunes) or on my android phone (with samsung music), which means this approach doesn't work.
const NodeID3 = require('node-id3')
//getting the filename and fileAuthor, as well as creating the file on the server
let tags = {
title: filename,
composer: fileAuthor,
artist: fileAuthor,
remixArtist: fileAuthor,
conductor: fileAuthor,
originalArtist: fileAuthor,
}
let success = NodeID3.update(tags, toBeDownloadedFilePath)
console.log(success)
let readTags = NodeID3.read(toBeDownloadedFilePath)
console.log(readTags)
the ffmetadata approach
Here is the same part written with the ffmetadata approach :
const ffmetadata = require("ffmetadata");
//getting the filename and fileAuthor, as well as creating the file on the server
let tags = {
artist: fileAuthor,
}
ffmetadata.write(toBeDownloadedFilePath, tags, function(err) {
if (err) console.error("Error writing metadata", err);
else console.log("Data written");
});
and with this approach, I'm getting the error :
[mp3 # 0x7f90f8000000] Format mp3 detected only with low score of 1, misdetection possible!
[mp3 # 0x7f90f8000000] Failed to read frame size: Could not seek to 1026.
music1.mp3: Invalid argument
(music1.mp3 being my filename), and my mp3 file is perfectly recognised by all audio reader with which I tested it.
Thank you very much for your help.
So I finally found where was the problem (at least with the node-id3 approach) :
to better understand it, I'll add some details to the creating the file on server step.
Here was my unfunctional code :
const NodeID3 = require('node-id3')
const fs = require('fs-extra'); // file system
fs.ensureFileSync(toBeDownloadedFilePath); //because I need to create the file if it doesn't exist
const toBeDownloadedFile = fs.createWriteStream(toBeDownloadedFilePath);
//the way the audio stream is created isn't relevant, but if you're interested, it's a youtube stream, as per https://www.npmjs.com/package/youtube-audio-stream
let fileWriteStream = audioStream.pipe(toBeDownloadedFile)
let tags = {
title: filename,
composer: fileAuthor,
artist: fileAuthor,
remixArtist: fileAuthor,
conductor: fileAuthor,
originalArtist: fileAuthor,
}
let success = NodeID3.update(tags, toBeDownloadedFilePath)
console.log(success)
let readTags = NodeID3.read(toBeDownloadedFilePath)
console.log(readTags)
And the problem was that my tags were written but immediately deleted by the audiostream.pipe
the solution was therefore quite simple, and I ended up with this code :
const NodeID3 = require('node-id3')
const fs = require('fs-extra'); // file system
fs.ensureFileSync(toBeDownloadedFilePath); //because I need to create the file if it doesn't exist
const toBeDownloadedFile = fs.createWriteStream(toBeDownloadedFilePath);
let fileWriteStream = audiSstream.pipe(toBeDownloadedFile)
fileWriteStream.on('finish', () => {
let tags = {
title: filename,
artist: fileAuthor,
}
NodeID3.update(tags, toBeDownloadedFilePath)
//any additional action, in my case, send the file for a download
})
Hope this helps people with similar problem.

pdfmake does not include fonts / text in node.js

I have a problem with pdfmake. I would like to generate a PDF on a node.js server. I would like to load data from a database and draw a nice table and simply save it to a folder.
var pdfMakePrinter = require('pdfmake/src/printer');
...
var fonts = {
Roboto: {
normal: './fonts/Roboto-Regular.ttf',
bold: './fonts/Roboto-Medium.ttf',
italics: './fonts/Roboto-Italic.ttf',
bolditalics: './fonts/Roboto-Italic.ttf'
}
};
var PdfPrinter = require('pdfmake/src/printer');
var printer = new PdfPrinter(fonts);
var docDefinition = {
content: [
'First paragraph',
'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
]
};
var pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('pdf/basics.pdf')).on('finish', function () {
res.send(true);
});
The generated PDF is empty. If I add an image, it is inserted well. But no font is included. The path of the fonts (which are given in the sample) is right.
Has anyone an idea, why no fonts are embedded and how this can be done in node.js? There are no valid samples on the pdfmake documentation.
After some debugging, I found out, that the app crashes in fontWrapper.js in this funktion:
FontWrapper.prototype.getFont = function(index){
if(!this.pdfFonts[index]){
var pseudoName = this.name + index;
if(this.postscriptName){
delete this.pdfkitDoc._fontFamilies[this.postscriptName];
}
this.pdfFonts[index] = this.pdfkitDoc.font(this.path, pseudoName)._font; <-- Crash
if(!this.postscriptName){
this.postscriptName = this.pdfFonts[index].name;
}
}
return this.pdfFonts[index];
};
Does anyone have an idea?
TTF is not issue in your case you can use any font to generate a PDF on a node.js server.
inside pdfmake
TTFFont.open = function(filename, name) {
var contents;
contents = fs.readFileSync(filename);
return new TTFFont(contents, name);
};
on contents = fs.readFileSync(filename); this line
fs can't read file on given path
as per This conversation you have to put your fonts at root folder,
but problem is when we create font object we gives root path and this path is not working for fs.readFileSync this line so you have to exact path of font
add process.cwd().split('.meteor')[0] befor font path
I have created example for same functionality please this below link
https://github.com/daupawar/MeteorAsyncPdfmake

How to get a img file in LocalFolder and display the image

I used this code to get the image file in the localFolder and I encountered the below problem.
The problem is at
BitmapImage src is null even there is data in randomstream
any idea how to overcome this problem? Why BitmapImage is null??
string strFilenm = "SDraw-" + prodId.ToString() + ".png";
var folder_path = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Img");
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folder_path);
StorageFile storagefile = await folder.GetFileAsync(strFilenm);
StorageFile jpgFile = await folder.GetFileAsync(strFilenm);
IRandomAccessStream randomStream = await jpgFile.OpenAsync(FileAccessMode.Read);
randomStream.Seek(0);
BitmapImage src = new BitmapImage();
await src.SetSourceAsync(randomStream);
Image1.Source = src;
I'm not expert but i think to access file from App LocalFolder you should use something like this:
"ms-appx://LocalFolderName/ImageName.extension"
try writing the instance " BitmapImage src = new BitmapImage(); " at class level.
One reason can be that the code might be trying to access a file that does'nt exist.

Resources