Advertisement
Guest User

Untitled

a guest
May 24th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. public uploadFile(event): void {
  2. // Check if the user uploaded a second photo to delete the previous one
  3. if (this.event.main_pic) {
  4. this.storageService.deleteFile(this.mainPicPath);
  5. this.storageService.deleteFile(this.thPicPath);
  6. }
  7. const file = event.target.files[0];
  8. const filePath = `events/${this.event.id}`;
  9. const fileNameNoExtension = file.name.substring(0, file.name.lastIndexOf('.'));
  10. const fileExtension = file.name.split('.').pop();
  11. const fileNameUrlified = this.urlifyService.urlify(fileNameNoExtension);
  12. const finalName = `${fileNameUrlified}.${fileExtension}`;
  13. this.uploadingFile = true;
  14.  
  15. // Calls the function below
  16. this.storageService.uploadFile(finalName, filePath, file, this.event.id)
  17. .then(response => {
  18. // Saves the path of the two photos created in case we need to delete it
  19. this.mainPicPath = `/${filePath}/thumb_${finalName}`;
  20. this.thPicPath = `/${filePath}/smallThumb_${finalName}`;
  21.  
  22. // Here's the two pieces of code that returns 404 not found
  23. this.storage.storage.ref(this.mainPicPath).getDownloadURL().then(data => {
  24. this.event.main_pic = data;
  25. })
  26. this.storage.storage.ref(this.thPicPath).getDownloadURL().then(data => {
  27. this.event.th_main_pic = data;
  28. })
  29. this.uploadingFile = false;
  30. })
  31. .catch(console.log);
  32. }
  33.  
  34. public uploadFile(finalName: string, filePath: string, file: any, id: string): Promise<any> {
  35. const storageFunctionURL = '*********/generateThumbs';
  36. return this.storage.upload(filePath + '/' + finalName, file).then(() => {
  37. return this.httpClient.get(storageFunctionURL, {
  38. params: {
  39. id: id,
  40. filePath: filePath,
  41. pic_name: finalName,
  42. contentType: file.type
  43. }
  44. })
  45. .toPromise();
  46. });
  47. }
  48.  
  49. exports.generateThumbs = functions.https.onRequest((req, res) => {
  50. const bucket = admin.storage().bucket();
  51. const fileName = req.query.pic_name;
  52. const user_id = req.query.id;
  53. const filePath = `/${req.query.filePath}/${fileName}`;
  54. const tempFilePath = path.join(os.tmpdir(), fileName);
  55. const contentType = req.query.contentType;
  56. const thumbFilePath = filePath.replace(/(/)?([^/]*)$/,'$1thumb_$2');
  57. const smallThumbFilePath = filePath.replace(/(/)?([^/]*)$/,'$1smallThumb_$2');
  58. let thumbHeight: number = 172;
  59. let smallThumbHeight: number = 50;
  60.  
  61.  
  62. if(filePath.startsWith('/events/')){
  63. thumbHeight = 1200;
  64. smallThumbHeight = 275;
  65. }
  66.  
  67. if(!contentType.startsWith('image/')){
  68. return null;
  69. }
  70.  
  71. return bucket.file(filePath).download({
  72. destination: tempFilePath
  73. })
  74. .then(()=>{
  75. let newFileTemp = path.join(os.tmpdir(), 'niufiletemp_'+fileName);
  76. sharp(tempFilePath)
  77. .resize(thumbHeight, null)
  78. .toFile(newFileTemp, (err, info) => {
  79. return bucket.upload(newFileTemp, {
  80. destination: thumbFilePath
  81. });
  82. })
  83. })
  84. .then((results) =>{
  85. let newFileTemp = path.join(os.tmpdir(), 'niufiletemp2_'+fileName);
  86. sharp(tempFilePath)
  87. .resize(smallThumbHeight, null)
  88. .toFile(newFileTemp, (err, info) => {
  89. return bucket.upload(newFileTemp, {
  90. destination: smallThumbFilePath
  91. })
  92. .then(() => {
  93. cors(req, res, () => {
  94. res.status(200).send({data: "upload finished"});
  95. return bucket.file(filePath).delete();
  96. })
  97. })
  98. })
  99. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement