-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refatoração no compartilhamento de código para abrir espaço a arquivo…
…s pessoais
- Loading branch information
Showing
3 changed files
with
66 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { inject, Injectable } from "@angular/core"; | ||
import { getBlob, ref, Storage, uploadString } from "@angular/fire/storage"; | ||
|
||
@Injectable({ providedIn: "root" }) | ||
export class ShareService { | ||
storage = inject(Storage); | ||
|
||
async share(code: string): Promise<string | null> { | ||
const shareId = (Math.random() + 1).toString(36).slice(2, 9); | ||
|
||
try { | ||
await uploadString(ref(this.storage, `share/${shareId}.por`), code, undefined, { | ||
contentType: "text/plain", | ||
}); | ||
|
||
return `https://portugol.dev/#share=${shareId}`; | ||
} catch (error) { | ||
console.error(error); | ||
return null; | ||
} | ||
} | ||
|
||
async load(shareId: string): Promise<string | null> { | ||
try { | ||
const oldVersionRef = ref(this.storage, shareId); | ||
const newVersionRef = ref(this.storage, `share/${shareId}.por`); | ||
|
||
// TODO: Remover oldVersionRef após 11/11/2024 | ||
const data = await getBlob(newVersionRef).catch(() => getBlob(oldVersionRef)); | ||
const contents = await data.text(); | ||
|
||
return contents; | ||
} catch (error) { | ||
console.error(error); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters