CarrierWave object update with existing file url - ruby

I'm using CarrierWave for file upload in my project. At the beginning of the implementation process, we've made a mistake and eventually a bunch of objects been uploaded to s3 with minor file naming issues. This was fixed shortly for new uploads.
Now we need to fix all this (let's say thousands) CarierWave/s3 objects broken paths.
Is there any good way to change CarrierWave object with no reuploading?
The part with s3 path update is already done, all I need to do is to change CarrierWave object.
Tried:
img.update(...)
img.raw_attributes_update(...)
...
img.file=...
(with further internal updates to define 'skip' upload var)
Note: re-uploading of all the images is our worst scenario, which I
really want to avoid.

Related

File::make('Fichier', 'path')->storeOriginalName('original_name') gives me gEUBH18u1UdjoPBS0POQB64xit5PF05Rnl8M5lMI.pdf

I'm using Laravel Nova,
I want to upload a file to S3. As this file is generated automatically, I know it has already a good naming. So, I want to keep original filename. But when using:
File::make('Fichier', 'path')
->disk('s3')
->storeOriginalName('path')
I realized that my file, originally named FILE1.pdf now is named gEUBH18u1UdjoPBS0POQB64xit5PF05Rnl8M5lMI.pdf
Why ?

Is there a right way to add an existing SQLite database to a Xamarin project?

Every example I've found creates the database for you and then has you create tables and populate them in code. My problem, though, is that I would like to create and populate the database elsewhere (SQLiteStudio) and then include it in my app.
I sense (through the general feel of ...whatever I've been looking at. We'll call it documentation) that you are supposed to copy the database to the Environment.SpecialFolder.Personal directory. So my workflow is to include the database as a resource and then copy it to the Environment.SpecialFolder.Personal directory. Is that right? Has anyone written any of this down succinctly and authoritatively (as opposed to loose collections of articles)?
I'd prefer not to have two copies of the same database but if that's what everyone else is doing then ...okay.
I have not been able to find an answer on any of the following web pages.
https://github.com/xamarin/recipes/tree/master/Recipes/ios/data/sqlite/create_a_database_with_sqlitenet
https://forums.xamarin.com/discussion/8188/creating-database-with-sqlite-only-once
https://github.com/praeclarum/sqlite-net
https://github.com/praeclarum/sqlite-net/wiki/GettingStarted
https://forums.xamarin.com/discussion/3773/system-environment-specialfolder
https://forums.xamarin.com/discussion/36285/where-do-you-store-your-sqlite-database-in-the-app
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/databases
Since you tagged pcl, have you tried treating this as an embedded resource? You pretty much just make a folder, drop in the database, and set the build action as an embedded resource. You can access the file through your SQLite library by linking up to the path of where the database is.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs=vswin

Uploading images to Spring Boot and S3 all In-Memory

I have an Angular webapp that uses a Spring Boot REST service as its backing web service.
I am adding a "Profiles" feature for users, and as part of this I want to stand up an endpoint that allows users to upload profile images for themselves and immediately upload those files to S3 (where I will host all the images from).
Looking at several Spring Boot/file upload tutorials :
http://www.mkyong.com/spring-boot/spring-boot-file-upload-example/
I update avatar image and display it but the avatar does not change in Spring Boot , why?
Many others
It seems that the standard way of handling such file upload is exposing a controller endpoint that accepts MultipartFiles like so:
#RestController
#RequestMapping("/v1/profiles")
public class ProfileController {
#PostMapping("/photo")
public ResponseEntity uploadProfilePhoto(#RequestParam("mpf") MultipartFile mpf)
// ...
}
Looking at all this code, I can't tell if the MultipartFile instance is in-memory or if Spring sets its location somewhere (perhaps under /tmp?) on the disk.
Looking at the AWS S3 Java SDK tutorial, it seems the standard way to upload a disk-based File is like so:
File file = new File(uploadFileName);
s3client.putObject(new PutObjectRequest(bucketName, keyName, file));
So it looks like I must have a File on disk in order to upload to S3.
I'm wondering if there is a way to keep everything in memory, or whether this is a bad idea and I should stick to disks/File instances!
Is there a way to keep the entire profile image (MultipartFile) in-mempory inside the controller method?
Is there a way to feed (maybe via serialization?!) a MultipartFile instance to S3's PutObjectRequest?
Or is this all a terrible idea (if so, why?!)?
Is there a way to keep the entire profile image (MultipartFile) in-mempory inside the controller method?
No, there is NO way to keep an image File in-memory because File object in java represents a path in file system.
Is there a way to feed (maybe via serialization?!) a MultipartFile instance to S3's PutObjectRequest?
No, from S3's API documentation, there is no way for S3 to deserialize to the image file for you after/during the upload.
Or is this all a terrible idea (if so, why?!)?
It depends on your specific case but it is generally not preferred.
If - there are not many users uploading images at the same time, your memory is probably enough to handle.
Else - You can easily get out-of-memory problems.
If you insist on doing so, S3 API can upload an InputStream (If I remember correctly). You can convert your Multipart File to an InputStream.
This SO thread talks about uploading to S3 with InputStream
You can also take a look at File.createTempFile() to create a temp file.
I have been looking at the same thing. Basically you want a user to be able to be able to upload a photo album and have those photos served from S3 and probably have them secured so only that user can upload/delete/etc.
I believe the simpler answer is in spring boot to get a Pre-signed URL from S3. https://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObjectJavaSDK.html
which basically gives you a token defining the bucket, and object key ("/bobs_profile/smiling_bob.jpg") and a time limit for that image to be uploaded.
Give that to your angular app (or ionic app) to upload the image to that location.
That should do it. but someone let me know if I'm wrong.
The only issue that I see is if bob wants to upload "bobs_nude_photo.jpg" and only wants spring security logged in people to be able to see it... well I'm sure there is an S3 solution for that??

Symfony Gaufrette create a copy of an image

I'm using a Symfony Gaufrette for storing images in a filesystem. It works fine when I store an image first time. But I also need to create a copy of the image after it was uploaded to the filesystem. Is there any way to do it?
I use this function for writing:
$filesystem->write($imagepath, file_get_contents($answer->get('imageObject')->getData()));
I was fighting all day long to make Gaufrette work with my Symfony2 app and Amazon S3 ... whenever I've made a step forward I had to take another fight ...
Finally I've switched to Flysystem and all my problems were gone ...
It's much better written and it just works (although config is almost the same).
Unfortunately copy() method now is not implemented in Gaufrette(you can see it in Adapter interface), so the only way to copy file using this library is to use read() and write() methods.

Creating an encrypted save file

I'm trying to make a game in Ruby, and my latest addition is to save files, however, I'm having a major problem with encrypting them.
I already got the saving and loading methods set up, but I don't want to save them in plain-text, because that's begging for cheating. No matter what, the methods I use to try and store them encrypted, compressed, etc., all seem to spring some error on me.
What's the best method for saving text in a file? The only important thing is that the file can't be opened and edited.
You can use crypt gem to encrypt/decrypt your file.
But keep in mind that if your users can view your Ruby script, there's basically no way to hide your data, since any encryptions requires some kind of keys.

Resources